aoptx86.pas 181 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aoptx86;
  18. {$i fpcdefs.inc}
  19. {$define DEBUG_AOPTCPU}
  20. interface
  21. uses
  22. globtype,
  23. cpubase,
  24. aasmtai,aasmcpu,
  25. cgbase,cgutils,
  26. aopt,aoptobj;
  27. type
  28. TX86AsmOptimizer = class(TAsmOptimizer)
  29. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  30. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  31. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  32. function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  33. function GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  34. protected
  35. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  36. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  37. { checks whether reading the value in reg1 depends on the value of reg2. This
  38. is very similar to SuperRegisterEquals, except it takes into account that
  39. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  40. depend on the value in AH). }
  41. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  42. procedure DebugMsg(const s : string; p : tai);inline;
  43. class function IsExitCode(p : tai) : boolean;
  44. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean;
  45. procedure RemoveLastDeallocForFuncRes(p : tai);
  46. function DoSubAddOpt(var p : tai) : Boolean;
  47. function PrePeepholeOptSxx(var p : tai) : boolean;
  48. function PrePeepholeOptIMUL(var p : tai) : boolean;
  49. function OptPass1AND(var p : tai) : boolean;
  50. function OptPass1_V_MOVAP(var p : tai) : boolean;
  51. function OptPass1VOP(var p : tai) : boolean;
  52. function OptPass1MOV(var p : tai) : boolean;
  53. function OptPass1Movx(var p : tai) : boolean;
  54. function OptPass1MOVXX(var p : tai) : boolean;
  55. function OptPass1OP(var p : tai) : boolean;
  56. function OptPass1LEA(var p : tai) : boolean;
  57. function OptPass1Sub(var p : tai) : boolean;
  58. function OptPass1SHLSAL(var p : tai) : boolean;
  59. function OptPass1SETcc(var p: tai): boolean;
  60. function OptPass1FSTP(var p: tai): boolean;
  61. function OptPass1FLD(var p: tai): boolean;
  62. function OptPass2MOV(var p : tai) : boolean;
  63. function OptPass2Imul(var p : tai) : boolean;
  64. function OptPass2Jmp(var p : tai) : boolean;
  65. function OptPass2Jcc(var p : tai) : boolean;
  66. function PostPeepholeOptMov(var p : tai) : Boolean;
  67. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  68. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  69. function PostPeepholeOptXor(var p : tai) : Boolean;
  70. {$endif}
  71. function PostPeepholeOptCmp(var p : tai) : Boolean;
  72. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  73. function PostPeepholeOptCall(var p : tai) : Boolean;
  74. function PostPeepholeOptLea(var p : tai) : Boolean;
  75. procedure OptReferences;
  76. end;
  77. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  78. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  79. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  80. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  81. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  82. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  83. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  84. function RefsEqual(const r1, r2: treference): boolean;
  85. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  86. { returns true, if ref is a reference using only the registers passed as base and index
  87. and having an offset }
  88. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  89. {$ifdef DEBUG_AOPTCPU}
  90. const
  91. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  92. {$else DEBUG_AOPTCPU}
  93. { Empty strings help the optimizer to remove string concatenations that won't
  94. ever appear to the user on release builds. [Kit] }
  95. const
  96. SPeepholeOptimization = '';
  97. {$endif DEBUG_AOPTCPU}
  98. implementation
  99. uses
  100. cutils,verbose,
  101. globals,
  102. cpuinfo,
  103. procinfo,
  104. aasmbase,
  105. aoptutils,
  106. symconst,symsym,
  107. cgx86,
  108. itcpugas;
  109. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  110. begin
  111. result :=
  112. (instr.typ = ait_instruction) and
  113. (taicpu(instr).opcode = op) and
  114. ((opsize = []) or (taicpu(instr).opsize in opsize));
  115. end;
  116. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  117. begin
  118. result :=
  119. (instr.typ = ait_instruction) and
  120. ((taicpu(instr).opcode = op1) or
  121. (taicpu(instr).opcode = op2)
  122. ) and
  123. ((opsize = []) or (taicpu(instr).opsize in opsize));
  124. end;
  125. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  126. begin
  127. result :=
  128. (instr.typ = ait_instruction) and
  129. ((taicpu(instr).opcode = op1) or
  130. (taicpu(instr).opcode = op2) or
  131. (taicpu(instr).opcode = op3)
  132. ) and
  133. ((opsize = []) or (taicpu(instr).opsize in opsize));
  134. end;
  135. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  136. const opsize : topsizes) : boolean;
  137. var
  138. op : TAsmOp;
  139. begin
  140. result:=false;
  141. for op in ops do
  142. begin
  143. if (instr.typ = ait_instruction) and
  144. (taicpu(instr).opcode = op) and
  145. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  146. begin
  147. result:=true;
  148. exit;
  149. end;
  150. end;
  151. end;
  152. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  153. begin
  154. result := (oper.typ = top_reg) and (oper.reg = reg);
  155. end;
  156. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  157. begin
  158. result := (oper.typ = top_const) and (oper.val = a);
  159. end;
  160. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  161. begin
  162. result := oper1.typ = oper2.typ;
  163. if result then
  164. case oper1.typ of
  165. top_const:
  166. Result:=oper1.val = oper2.val;
  167. top_reg:
  168. Result:=oper1.reg = oper2.reg;
  169. top_ref:
  170. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  171. else
  172. internalerror(2013102801);
  173. end
  174. end;
  175. function RefsEqual(const r1, r2: treference): boolean;
  176. begin
  177. RefsEqual :=
  178. (r1.offset = r2.offset) and
  179. (r1.segment = r2.segment) and (r1.base = r2.base) and
  180. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  181. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  182. (r1.relsymbol = r2.relsymbol) and
  183. (r1.volatility=[]) and
  184. (r2.volatility=[]);
  185. end;
  186. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  187. begin
  188. Result:=(ref.offset=0) and
  189. (ref.scalefactor in [0,1]) and
  190. (ref.segment=NR_NO) and
  191. (ref.symbol=nil) and
  192. (ref.relsymbol=nil) and
  193. ((base=NR_INVALID) or
  194. (ref.base=base)) and
  195. ((index=NR_INVALID) or
  196. (ref.index=index)) and
  197. (ref.volatility=[]);
  198. end;
  199. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  200. begin
  201. Result:=(ref.scalefactor in [0,1]) and
  202. (ref.segment=NR_NO) and
  203. (ref.symbol=nil) and
  204. (ref.relsymbol=nil) and
  205. ((base=NR_INVALID) or
  206. (ref.base=base)) and
  207. ((index=NR_INVALID) or
  208. (ref.index=index)) and
  209. (ref.volatility=[]);
  210. end;
  211. function InstrReadsFlags(p: tai): boolean;
  212. begin
  213. InstrReadsFlags := true;
  214. case p.typ of
  215. ait_instruction:
  216. if InsProp[taicpu(p).opcode].Ch*
  217. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  218. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  219. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  220. exit;
  221. ait_label:
  222. exit;
  223. else
  224. ;
  225. end;
  226. InstrReadsFlags := false;
  227. end;
  228. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  229. begin
  230. Next:=Current;
  231. repeat
  232. Result:=GetNextInstruction(Next,Next);
  233. until not (Result) or
  234. not(cs_opt_level3 in current_settings.optimizerswitches) or
  235. (Next.typ<>ait_instruction) or
  236. RegInInstruction(reg,Next) or
  237. is_calljmp(taicpu(Next).opcode);
  238. end;
  239. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  240. begin
  241. Result:=RegReadByInstruction(reg,hp);
  242. end;
  243. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  244. var
  245. p: taicpu;
  246. opcount: longint;
  247. begin
  248. RegReadByInstruction := false;
  249. if hp.typ <> ait_instruction then
  250. exit;
  251. p := taicpu(hp);
  252. case p.opcode of
  253. A_CALL:
  254. regreadbyinstruction := true;
  255. A_IMUL:
  256. case p.ops of
  257. 1:
  258. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  259. (
  260. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  261. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  262. );
  263. 2,3:
  264. regReadByInstruction :=
  265. reginop(reg,p.oper[0]^) or
  266. reginop(reg,p.oper[1]^);
  267. end;
  268. A_MUL:
  269. begin
  270. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  271. (
  272. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  273. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  274. );
  275. end;
  276. A_IDIV,A_DIV:
  277. begin
  278. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  279. (
  280. (getregtype(reg)=R_INTREGISTER) and
  281. (
  282. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  283. )
  284. );
  285. end;
  286. else
  287. begin
  288. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  289. begin
  290. RegReadByInstruction := false;
  291. exit;
  292. end;
  293. for opcount := 0 to p.ops-1 do
  294. if (p.oper[opCount]^.typ = top_ref) and
  295. RegInRef(reg,p.oper[opcount]^.ref^) then
  296. begin
  297. RegReadByInstruction := true;
  298. exit
  299. end;
  300. { special handling for SSE MOVSD }
  301. if (p.opcode=A_MOVSD) and (p.ops>0) then
  302. begin
  303. if p.ops<>2 then
  304. internalerror(2017042702);
  305. regReadByInstruction := reginop(reg,p.oper[0]^) or
  306. (
  307. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  308. );
  309. exit;
  310. end;
  311. with insprop[p.opcode] do
  312. begin
  313. if getregtype(reg)=R_INTREGISTER then
  314. begin
  315. case getsupreg(reg) of
  316. RS_EAX:
  317. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  318. begin
  319. RegReadByInstruction := true;
  320. exit
  321. end;
  322. RS_ECX:
  323. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  324. begin
  325. RegReadByInstruction := true;
  326. exit
  327. end;
  328. RS_EDX:
  329. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  330. begin
  331. RegReadByInstruction := true;
  332. exit
  333. end;
  334. RS_EBX:
  335. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  336. begin
  337. RegReadByInstruction := true;
  338. exit
  339. end;
  340. RS_ESP:
  341. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  342. begin
  343. RegReadByInstruction := true;
  344. exit
  345. end;
  346. RS_EBP:
  347. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  348. begin
  349. RegReadByInstruction := true;
  350. exit
  351. end;
  352. RS_ESI:
  353. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  354. begin
  355. RegReadByInstruction := true;
  356. exit
  357. end;
  358. RS_EDI:
  359. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  360. begin
  361. RegReadByInstruction := true;
  362. exit
  363. end;
  364. end;
  365. end;
  366. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  367. begin
  368. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  369. begin
  370. case p.condition of
  371. C_A,C_NBE, { CF=0 and ZF=0 }
  372. C_BE,C_NA: { CF=1 or ZF=1 }
  373. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  374. C_AE,C_NB,C_NC, { CF=0 }
  375. C_B,C_NAE,C_C: { CF=1 }
  376. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  377. C_NE,C_NZ, { ZF=0 }
  378. C_E,C_Z: { ZF=1 }
  379. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  380. C_G,C_NLE, { ZF=0 and SF=OF }
  381. C_LE,C_NG: { ZF=1 or SF<>OF }
  382. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  383. C_GE,C_NL, { SF=OF }
  384. C_L,C_NGE: { SF<>OF }
  385. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  386. C_NO, { OF=0 }
  387. C_O: { OF=1 }
  388. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  389. C_NP,C_PO, { PF=0 }
  390. C_P,C_PE: { PF=1 }
  391. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  392. C_NS, { SF=0 }
  393. C_S: { SF=1 }
  394. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  395. else
  396. internalerror(2017042701);
  397. end;
  398. if RegReadByInstruction then
  399. exit;
  400. end;
  401. case getsubreg(reg) of
  402. R_SUBW,R_SUBD,R_SUBQ:
  403. RegReadByInstruction :=
  404. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  405. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  406. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  407. R_SUBFLAGCARRY:
  408. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  409. R_SUBFLAGPARITY:
  410. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  411. R_SUBFLAGAUXILIARY:
  412. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  413. R_SUBFLAGZERO:
  414. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  415. R_SUBFLAGSIGN:
  416. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  417. R_SUBFLAGOVERFLOW:
  418. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  419. R_SUBFLAGINTERRUPT:
  420. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  421. R_SUBFLAGDIRECTION:
  422. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  423. else
  424. internalerror(2017042601);
  425. end;
  426. exit;
  427. end;
  428. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  429. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  430. (p.oper[0]^.reg=p.oper[1]^.reg) then
  431. exit;
  432. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  433. begin
  434. RegReadByInstruction := true;
  435. exit
  436. end;
  437. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  438. begin
  439. RegReadByInstruction := true;
  440. exit
  441. end;
  442. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  443. begin
  444. RegReadByInstruction := true;
  445. exit
  446. end;
  447. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  448. begin
  449. RegReadByInstruction := true;
  450. exit
  451. end;
  452. end;
  453. end;
  454. end;
  455. end;
  456. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  457. begin
  458. result:=false;
  459. if p1.typ<>ait_instruction then
  460. exit;
  461. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  462. exit(true);
  463. if (getregtype(reg)=R_INTREGISTER) and
  464. { change information for xmm movsd are not correct }
  465. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  466. begin
  467. case getsupreg(reg) of
  468. { RS_EAX = RS_RAX on x86-64 }
  469. RS_EAX:
  470. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  471. RS_ECX:
  472. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  473. RS_EDX:
  474. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  475. RS_EBX:
  476. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  477. RS_ESP:
  478. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  479. RS_EBP:
  480. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  481. RS_ESI:
  482. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  483. RS_EDI:
  484. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  485. else
  486. ;
  487. end;
  488. if result then
  489. exit;
  490. end
  491. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  492. begin
  493. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  494. exit(true);
  495. case getsubreg(reg) of
  496. R_SUBFLAGCARRY:
  497. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  498. R_SUBFLAGPARITY:
  499. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  500. R_SUBFLAGAUXILIARY:
  501. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  502. R_SUBFLAGZERO:
  503. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  504. R_SUBFLAGSIGN:
  505. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  506. R_SUBFLAGOVERFLOW:
  507. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  508. R_SUBFLAGINTERRUPT:
  509. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  510. R_SUBFLAGDIRECTION:
  511. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  512. else
  513. ;
  514. end;
  515. if result then
  516. exit;
  517. end
  518. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  519. exit(true);
  520. Result:=inherited RegInInstruction(Reg, p1);
  521. end;
  522. {$ifdef DEBUG_AOPTCPU}
  523. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  524. begin
  525. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  526. end;
  527. function debug_tostr(i: tcgint): string; inline;
  528. begin
  529. Result := tostr(i);
  530. end;
  531. function debug_regname(r: TRegister): string; inline;
  532. begin
  533. Result := '%' + std_regname(r);
  534. end;
  535. { Debug output function - creates a string representation of an operator }
  536. function debug_operstr(oper: TOper): string;
  537. begin
  538. case oper.typ of
  539. top_const:
  540. Result := '$' + debug_tostr(oper.val);
  541. top_reg:
  542. Result := debug_regname(oper.reg);
  543. top_ref:
  544. begin
  545. if oper.ref^.offset <> 0 then
  546. Result := debug_tostr(oper.ref^.offset) + '('
  547. else
  548. Result := '(';
  549. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  550. begin
  551. Result := Result + debug_regname(oper.ref^.base);
  552. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  553. Result := Result + ',' + debug_regname(oper.ref^.index);
  554. end
  555. else
  556. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  557. Result := Result + debug_regname(oper.ref^.index);
  558. if (oper.ref^.scalefactor > 1) then
  559. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  560. else
  561. Result := Result + ')';
  562. end;
  563. else
  564. Result := '[UNKNOWN]';
  565. end;
  566. end;
  567. function debug_op2str(opcode: tasmop): string; inline;
  568. begin
  569. Result := std_op2str[opcode];
  570. end;
  571. function debug_opsize2str(opsize: topsize): string; inline;
  572. begin
  573. Result := gas_opsize2str[opsize];
  574. end;
  575. {$else DEBUG_AOPTCPU}
  576. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  577. begin
  578. end;
  579. function debug_tostr(i: tcgint): string; inline;
  580. begin
  581. Result := '';
  582. end;
  583. function debug_regname(r: TRegister): string; inline;
  584. begin
  585. Result := '';
  586. end;
  587. function debug_operstr(oper: TOper): string; inline;
  588. begin
  589. Result := '';
  590. end;
  591. function debug_op2str(opcode: tasmop): string; inline;
  592. begin
  593. Result := '';
  594. end;
  595. function debug_opsize2str(opsize: topsize): string; inline;
  596. begin
  597. Result := '';
  598. end;
  599. {$endif DEBUG_AOPTCPU}
  600. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  601. begin
  602. if not SuperRegistersEqual(reg1,reg2) then
  603. exit(false);
  604. if getregtype(reg1)<>R_INTREGISTER then
  605. exit(true); {because SuperRegisterEqual is true}
  606. case getsubreg(reg1) of
  607. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  608. higher, it preserves the high bits, so the new value depends on
  609. reg2's previous value. In other words, it is equivalent to doing:
  610. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  611. R_SUBL:
  612. exit(getsubreg(reg2)=R_SUBL);
  613. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  614. higher, it actually does a:
  615. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  616. R_SUBH:
  617. exit(getsubreg(reg2)=R_SUBH);
  618. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  619. bits of reg2:
  620. reg2 := (reg2 and $ffff0000) or word(reg1); }
  621. R_SUBW:
  622. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  623. { a write to R_SUBD always overwrites every other subregister,
  624. because it clears the high 32 bits of R_SUBQ on x86_64 }
  625. R_SUBD,
  626. R_SUBQ:
  627. exit(true);
  628. else
  629. internalerror(2017042801);
  630. end;
  631. end;
  632. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  633. begin
  634. if not SuperRegistersEqual(reg1,reg2) then
  635. exit(false);
  636. if getregtype(reg1)<>R_INTREGISTER then
  637. exit(true); {because SuperRegisterEqual is true}
  638. case getsubreg(reg1) of
  639. R_SUBL:
  640. exit(getsubreg(reg2)<>R_SUBH);
  641. R_SUBH:
  642. exit(getsubreg(reg2)<>R_SUBL);
  643. R_SUBW,
  644. R_SUBD,
  645. R_SUBQ:
  646. exit(true);
  647. else
  648. internalerror(2017042802);
  649. end;
  650. end;
  651. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  652. var
  653. hp1 : tai;
  654. l : TCGInt;
  655. begin
  656. result:=false;
  657. { changes the code sequence
  658. shr/sar const1, x
  659. shl const2, x
  660. to
  661. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  662. if GetNextInstruction(p, hp1) and
  663. MatchInstruction(hp1,A_SHL,[]) and
  664. (taicpu(p).oper[0]^.typ = top_const) and
  665. (taicpu(hp1).oper[0]^.typ = top_const) and
  666. (taicpu(hp1).opsize = taicpu(p).opsize) and
  667. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  668. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  669. begin
  670. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  671. not(cs_opt_size in current_settings.optimizerswitches) then
  672. begin
  673. { shr/sar const1, %reg
  674. shl const2, %reg
  675. with const1 > const2 }
  676. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  677. taicpu(hp1).opcode := A_AND;
  678. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  679. case taicpu(p).opsize Of
  680. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  681. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  682. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  683. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  684. else
  685. Internalerror(2017050703)
  686. end;
  687. end
  688. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  689. not(cs_opt_size in current_settings.optimizerswitches) then
  690. begin
  691. { shr/sar const1, %reg
  692. shl const2, %reg
  693. with const1 < const2 }
  694. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  695. taicpu(p).opcode := A_AND;
  696. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  697. case taicpu(p).opsize Of
  698. S_B: taicpu(p).loadConst(0,l Xor $ff);
  699. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  700. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  701. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  702. else
  703. Internalerror(2017050702)
  704. end;
  705. end
  706. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  707. begin
  708. { shr/sar const1, %reg
  709. shl const2, %reg
  710. with const1 = const2 }
  711. taicpu(p).opcode := A_AND;
  712. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  713. case taicpu(p).opsize Of
  714. S_B: taicpu(p).loadConst(0,l Xor $ff);
  715. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  716. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  717. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  718. else
  719. Internalerror(2017050701)
  720. end;
  721. asml.remove(hp1);
  722. hp1.free;
  723. end;
  724. end;
  725. end;
  726. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  727. var
  728. opsize : topsize;
  729. hp1 : tai;
  730. tmpref : treference;
  731. ShiftValue : Cardinal;
  732. BaseValue : TCGInt;
  733. begin
  734. result:=false;
  735. opsize:=taicpu(p).opsize;
  736. { changes certain "imul const, %reg"'s to lea sequences }
  737. if (MatchOpType(taicpu(p),top_const,top_reg) or
  738. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  739. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  740. if (taicpu(p).oper[0]^.val = 1) then
  741. if (taicpu(p).ops = 2) then
  742. { remove "imul $1, reg" }
  743. begin
  744. hp1 := tai(p.Next);
  745. asml.remove(p);
  746. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  747. p.free;
  748. p := hp1;
  749. result:=true;
  750. end
  751. else
  752. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  753. begin
  754. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  755. InsertLLItem(p.previous, p.next, hp1);
  756. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  757. p.free;
  758. p := hp1;
  759. end
  760. else if
  761. ((taicpu(p).ops <= 2) or
  762. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  763. not(cs_opt_size in current_settings.optimizerswitches) and
  764. (not(GetNextInstruction(p, hp1)) or
  765. not((tai(hp1).typ = ait_instruction) and
  766. ((taicpu(hp1).opcode=A_Jcc) and
  767. (taicpu(hp1).condition in [C_O,C_NO])))) then
  768. begin
  769. {
  770. imul X, reg1, reg2 to
  771. lea (reg1,reg1,Y), reg2
  772. shl ZZ,reg2
  773. imul XX, reg1 to
  774. lea (reg1,reg1,YY), reg1
  775. shl ZZ,reg2
  776. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  777. it does not exist as a separate optimization target in FPC though.
  778. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  779. at most two zeros
  780. }
  781. reference_reset(tmpref,1,[]);
  782. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  783. begin
  784. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  785. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  786. TmpRef.base := taicpu(p).oper[1]^.reg;
  787. TmpRef.index := taicpu(p).oper[1]^.reg;
  788. if not(BaseValue in [3,5,9]) then
  789. Internalerror(2018110101);
  790. TmpRef.ScaleFactor := BaseValue-1;
  791. if (taicpu(p).ops = 2) then
  792. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  793. else
  794. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  795. AsmL.InsertAfter(hp1,p);
  796. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  797. AsmL.Remove(p);
  798. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  799. p.free;
  800. p := hp1;
  801. if ShiftValue>0 then
  802. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  803. end;
  804. end;
  805. end;
  806. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  807. var
  808. p: taicpu;
  809. begin
  810. if not assigned(hp) or
  811. (hp.typ <> ait_instruction) then
  812. begin
  813. Result := false;
  814. exit;
  815. end;
  816. p := taicpu(hp);
  817. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  818. with insprop[p.opcode] do
  819. begin
  820. case getsubreg(reg) of
  821. R_SUBW,R_SUBD,R_SUBQ:
  822. Result:=
  823. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  824. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  825. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  826. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  827. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  828. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  829. R_SUBFLAGCARRY:
  830. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  831. R_SUBFLAGPARITY:
  832. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  833. R_SUBFLAGAUXILIARY:
  834. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  835. R_SUBFLAGZERO:
  836. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  837. R_SUBFLAGSIGN:
  838. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  839. R_SUBFLAGOVERFLOW:
  840. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  841. R_SUBFLAGINTERRUPT:
  842. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  843. R_SUBFLAGDIRECTION:
  844. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  845. else
  846. begin
  847. writeln(getsubreg(reg));
  848. internalerror(2017050501);
  849. end;
  850. end;
  851. exit;
  852. end;
  853. Result :=
  854. (((p.opcode = A_MOV) or
  855. (p.opcode = A_MOVZX) or
  856. (p.opcode = A_MOVSX) or
  857. (p.opcode = A_LEA) or
  858. (p.opcode = A_VMOVSS) or
  859. (p.opcode = A_VMOVSD) or
  860. (p.opcode = A_VMOVAPD) or
  861. (p.opcode = A_VMOVAPS) or
  862. (p.opcode = A_VMOVQ) or
  863. (p.opcode = A_MOVSS) or
  864. (p.opcode = A_MOVSD) or
  865. (p.opcode = A_MOVQ) or
  866. (p.opcode = A_MOVAPD) or
  867. (p.opcode = A_MOVAPS) or
  868. {$ifndef x86_64}
  869. (p.opcode = A_LDS) or
  870. (p.opcode = A_LES) or
  871. {$endif not x86_64}
  872. (p.opcode = A_LFS) or
  873. (p.opcode = A_LGS) or
  874. (p.opcode = A_LSS)) and
  875. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  876. (p.oper[1]^.typ = top_reg) and
  877. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  878. ((p.oper[0]^.typ = top_const) or
  879. ((p.oper[0]^.typ = top_reg) and
  880. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  881. ((p.oper[0]^.typ = top_ref) and
  882. not RegInRef(reg,p.oper[0]^.ref^)))) or
  883. ((p.opcode = A_POP) and
  884. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg))) or
  885. ((p.opcode = A_IMUL) and
  886. (p.ops=3) and
  887. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  888. (((p.oper[1]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  889. ((p.oper[1]^.typ=top_ref) and not(RegInRef(reg,p.oper[1]^.ref^))))) or
  890. ((((p.opcode = A_IMUL) or
  891. (p.opcode = A_MUL)) and
  892. (p.ops=1)) and
  893. (((p.oper[0]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  894. ((p.oper[0]^.typ=top_ref) and not(RegInRef(reg,p.oper[0]^.ref^)))) and
  895. (((p.opsize=S_B) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  896. ((p.opsize=S_W) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  897. ((p.opsize=S_L) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg))
  898. {$ifdef x86_64}
  899. or ((p.opsize=S_Q) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg))
  900. {$endif x86_64}
  901. )) or
  902. ((p.opcode = A_CWD) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  903. ((p.opcode = A_CDQ) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)) or
  904. {$ifdef x86_64}
  905. ((p.opcode = A_CQO) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)) or
  906. {$endif x86_64}
  907. ((p.opcode = A_CBW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  908. {$ifndef x86_64}
  909. ((p.opcode = A_LDS) and (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  910. ((p.opcode = A_LES) and (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  911. {$endif not x86_64}
  912. ((p.opcode = A_LFS) and (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  913. ((p.opcode = A_LGS) and (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  914. ((p.opcode = A_LSS) and (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  915. {$ifndef x86_64}
  916. ((p.opcode = A_AAM) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  917. {$endif not x86_64}
  918. ((p.opcode = A_LAHF) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  919. ((p.opcode = A_LODSB) and Reg1WriteOverwritesReg2Entirely(NR_AL,reg)) or
  920. ((p.opcode = A_LODSW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg)) or
  921. ((p.opcode = A_LODSD) and Reg1WriteOverwritesReg2Entirely(NR_EAX,reg)) or
  922. {$ifdef x86_64}
  923. ((p.opcode = A_LODSQ) and Reg1WriteOverwritesReg2Entirely(NR_RAX,reg)) or
  924. {$endif x86_64}
  925. ((p.opcode = A_SETcc) and (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  926. (((p.opcode = A_FSTSW) or
  927. (p.opcode = A_FNSTSW)) and
  928. (p.oper[0]^.typ=top_reg) and
  929. Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  930. (((p.opcode = A_XOR) or (p.opcode = A_SUB) or (p.opcode = A_SBB)) and
  931. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  932. (p.oper[0]^.reg=p.oper[1]^.reg) and
  933. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg));
  934. end;
  935. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  936. var
  937. hp2,hp3 : tai;
  938. begin
  939. { some x86-64 issue a NOP before the real exit code }
  940. if MatchInstruction(p,A_NOP,[]) then
  941. GetNextInstruction(p,p);
  942. result:=assigned(p) and (p.typ=ait_instruction) and
  943. ((taicpu(p).opcode = A_RET) or
  944. ((taicpu(p).opcode=A_LEAVE) and
  945. GetNextInstruction(p,hp2) and
  946. MatchInstruction(hp2,A_RET,[S_NO])
  947. ) or
  948. (((taicpu(p).opcode=A_LEA) and
  949. MatchOpType(taicpu(p),top_ref,top_reg) and
  950. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  951. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  952. ) and
  953. GetNextInstruction(p,hp2) and
  954. MatchInstruction(hp2,A_RET,[S_NO])
  955. ) or
  956. ((((taicpu(p).opcode=A_MOV) and
  957. MatchOpType(taicpu(p),top_reg,top_reg) and
  958. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  959. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  960. ((taicpu(p).opcode=A_LEA) and
  961. MatchOpType(taicpu(p),top_ref,top_reg) and
  962. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  963. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  964. )
  965. ) and
  966. GetNextInstruction(p,hp2) and
  967. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  968. MatchOpType(taicpu(hp2),top_reg) and
  969. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  970. GetNextInstruction(hp2,hp3) and
  971. MatchInstruction(hp3,A_RET,[S_NO])
  972. )
  973. );
  974. end;
  975. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  976. begin
  977. isFoldableArithOp := False;
  978. case hp1.opcode of
  979. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  980. isFoldableArithOp :=
  981. ((taicpu(hp1).oper[0]^.typ = top_const) or
  982. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  983. (taicpu(hp1).oper[0]^.reg <> reg))) and
  984. (taicpu(hp1).oper[1]^.typ = top_reg) and
  985. (taicpu(hp1).oper[1]^.reg = reg);
  986. A_INC,A_DEC,A_NEG,A_NOT:
  987. isFoldableArithOp :=
  988. (taicpu(hp1).oper[0]^.typ = top_reg) and
  989. (taicpu(hp1).oper[0]^.reg = reg);
  990. else
  991. ;
  992. end;
  993. end;
  994. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  995. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  996. var
  997. hp2: tai;
  998. begin
  999. hp2 := p;
  1000. repeat
  1001. hp2 := tai(hp2.previous);
  1002. if assigned(hp2) and
  1003. (hp2.typ = ait_regalloc) and
  1004. (tai_regalloc(hp2).ratype=ra_dealloc) and
  1005. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  1006. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  1007. begin
  1008. asml.remove(hp2);
  1009. hp2.free;
  1010. break;
  1011. end;
  1012. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  1013. end;
  1014. begin
  1015. case current_procinfo.procdef.returndef.typ of
  1016. arraydef,recorddef,pointerdef,
  1017. stringdef,enumdef,procdef,objectdef,errordef,
  1018. filedef,setdef,procvardef,
  1019. classrefdef,forwarddef:
  1020. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1021. orddef:
  1022. if current_procinfo.procdef.returndef.size <> 0 then
  1023. begin
  1024. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1025. { for int64/qword }
  1026. if current_procinfo.procdef.returndef.size = 8 then
  1027. DoRemoveLastDeallocForFuncRes(RS_EDX);
  1028. end;
  1029. else
  1030. ;
  1031. end;
  1032. end;
  1033. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  1034. var
  1035. hp1,hp2 : tai;
  1036. begin
  1037. result:=false;
  1038. if MatchOpType(taicpu(p),top_reg,top_reg) then
  1039. begin
  1040. { vmova* reg1,reg1
  1041. =>
  1042. <nop> }
  1043. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1044. begin
  1045. GetNextInstruction(p,hp1);
  1046. asml.Remove(p);
  1047. p.Free;
  1048. p:=hp1;
  1049. result:=true;
  1050. end
  1051. else if GetNextInstruction(p,hp1) then
  1052. begin
  1053. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1054. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1055. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1056. begin
  1057. { vmova* reg1,reg2
  1058. vmova* reg2,reg3
  1059. dealloc reg2
  1060. =>
  1061. vmova* reg1,reg3 }
  1062. TransferUsedRegs(TmpUsedRegs);
  1063. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1064. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1065. begin
  1066. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1067. asml.Remove(hp1);
  1068. hp1.Free;
  1069. result:=true;
  1070. end
  1071. { special case:
  1072. vmova* reg1,reg2
  1073. vmova* reg2,reg1
  1074. =>
  1075. vmova* reg1,reg2 }
  1076. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
  1077. begin
  1078. asml.Remove(hp1);
  1079. hp1.Free;
  1080. result:=true;
  1081. end
  1082. end
  1083. else if MatchInstruction(hp1,[A_VFMADDPD,
  1084. A_VFMADD132PD,
  1085. A_VFMADD132PS,
  1086. A_VFMADD132SD,
  1087. A_VFMADD132SS,
  1088. A_VFMADD213PD,
  1089. A_VFMADD213PS,
  1090. A_VFMADD213SD,
  1091. A_VFMADD213SS,
  1092. A_VFMADD231PD,
  1093. A_VFMADD231PS,
  1094. A_VFMADD231SD,
  1095. A_VFMADD231SS,
  1096. A_VFMADDSUB132PD,
  1097. A_VFMADDSUB132PS,
  1098. A_VFMADDSUB213PD,
  1099. A_VFMADDSUB213PS,
  1100. A_VFMADDSUB231PD,
  1101. A_VFMADDSUB231PS,
  1102. A_VFMSUB132PD,
  1103. A_VFMSUB132PS,
  1104. A_VFMSUB132SD,
  1105. A_VFMSUB132SS,
  1106. A_VFMSUB213PD,
  1107. A_VFMSUB213PS,
  1108. A_VFMSUB213SD,
  1109. A_VFMSUB213SS,
  1110. A_VFMSUB231PD,
  1111. A_VFMSUB231PS,
  1112. A_VFMSUB231SD,
  1113. A_VFMSUB231SS,
  1114. A_VFMSUBADD132PD,
  1115. A_VFMSUBADD132PS,
  1116. A_VFMSUBADD213PD,
  1117. A_VFMSUBADD213PS,
  1118. A_VFMSUBADD231PD,
  1119. A_VFMSUBADD231PS,
  1120. A_VFNMADD132PD,
  1121. A_VFNMADD132PS,
  1122. A_VFNMADD132SD,
  1123. A_VFNMADD132SS,
  1124. A_VFNMADD213PD,
  1125. A_VFNMADD213PS,
  1126. A_VFNMADD213SD,
  1127. A_VFNMADD213SS,
  1128. A_VFNMADD231PD,
  1129. A_VFNMADD231PS,
  1130. A_VFNMADD231SD,
  1131. A_VFNMADD231SS,
  1132. A_VFNMSUB132PD,
  1133. A_VFNMSUB132PS,
  1134. A_VFNMSUB132SD,
  1135. A_VFNMSUB132SS,
  1136. A_VFNMSUB213PD,
  1137. A_VFNMSUB213PS,
  1138. A_VFNMSUB213SD,
  1139. A_VFNMSUB213SS,
  1140. A_VFNMSUB231PD,
  1141. A_VFNMSUB231PS,
  1142. A_VFNMSUB231SD,
  1143. A_VFNMSUB231SS],[S_NO]) and
  1144. { we mix single and double opperations here because we assume that the compiler
  1145. generates vmovapd only after double operations and vmovaps only after single operations }
  1146. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1147. GetNextInstruction(hp1,hp2) and
  1148. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  1149. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1150. begin
  1151. TransferUsedRegs(TmpUsedRegs);
  1152. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1153. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1154. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs))
  1155. then
  1156. begin
  1157. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1158. asml.Remove(p);
  1159. p.Free;
  1160. asml.Remove(hp2);
  1161. hp2.Free;
  1162. p:=hp1;
  1163. end;
  1164. end
  1165. else if (hp1.typ = ait_instruction) and
  1166. GetNextInstruction(hp1, hp2) and
  1167. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  1168. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1169. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1170. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  1171. (((taicpu(p).opcode=A_MOVAPS) and
  1172. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  1173. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  1174. ((taicpu(p).opcode=A_MOVAPD) and
  1175. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  1176. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  1177. ) then
  1178. { change
  1179. movapX reg,reg2
  1180. addsX/subsX/... reg3, reg2
  1181. movapX reg2,reg
  1182. to
  1183. addsX/subsX/... reg3,reg
  1184. }
  1185. begin
  1186. TransferUsedRegs(TmpUsedRegs);
  1187. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1188. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1189. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1190. begin
  1191. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  1192. debug_op2str(taicpu(p).opcode)+' '+
  1193. debug_op2str(taicpu(hp1).opcode)+' '+
  1194. debug_op2str(taicpu(hp2).opcode)+') done',p);
  1195. { we cannot eliminate the first move if
  1196. the operations uses the same register for source and dest }
  1197. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  1198. begin
  1199. asml.remove(p);
  1200. p.Free;
  1201. end;
  1202. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1203. asml.remove(hp2);
  1204. hp2.Free;
  1205. p:=hp1;
  1206. result:=true;
  1207. end;
  1208. end;
  1209. end;
  1210. end;
  1211. end;
  1212. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1213. var
  1214. hp1 : tai;
  1215. begin
  1216. result:=false;
  1217. { replace
  1218. V<Op>X %mreg1,%mreg2,%mreg3
  1219. VMovX %mreg3,%mreg4
  1220. dealloc %mreg3
  1221. by
  1222. V<Op>X %mreg1,%mreg2,%mreg4
  1223. ?
  1224. }
  1225. if GetNextInstruction(p,hp1) and
  1226. { we mix single and double operations here because we assume that the compiler
  1227. generates vmovapd only after double operations and vmovaps only after single operations }
  1228. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1229. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1230. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1231. begin
  1232. TransferUsedRegs(TmpUsedRegs);
  1233. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1234. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)
  1235. ) then
  1236. begin
  1237. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1238. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1239. asml.Remove(hp1);
  1240. hp1.Free;
  1241. result:=true;
  1242. end;
  1243. end;
  1244. end;
  1245. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  1246. var
  1247. hp1, hp2: tai;
  1248. GetNextInstruction_p: Boolean;
  1249. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  1250. NewSize: topsize;
  1251. begin
  1252. Result:=false;
  1253. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  1254. { remove mov reg1,reg1? }
  1255. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  1256. then
  1257. begin
  1258. DebugMsg(SPeepholeOptimization + 'Mov2Nop done',p);
  1259. { take care of the register (de)allocs following p }
  1260. UpdateUsedRegs(tai(p.next));
  1261. asml.remove(p);
  1262. p.free;
  1263. p:=hp1;
  1264. Result:=true;
  1265. exit;
  1266. end;
  1267. if GetNextInstruction_p and
  1268. MatchInstruction(hp1,A_AND,[]) and
  1269. (taicpu(p).oper[1]^.typ = top_reg) and
  1270. MatchOpType(taicpu(hp1),top_const,top_reg) then
  1271. begin
  1272. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  1273. begin
  1274. case taicpu(p).opsize of
  1275. S_L:
  1276. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  1277. begin
  1278. { Optimize out:
  1279. mov x, %reg
  1280. and ffffffffh, %reg
  1281. }
  1282. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  1283. asml.remove(hp1);
  1284. hp1.free;
  1285. Result:=true;
  1286. exit;
  1287. end;
  1288. S_Q: { TODO: Confirm if this is even possible }
  1289. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  1290. begin
  1291. { Optimize out:
  1292. mov x, %reg
  1293. and ffffffffffffffffh, %reg
  1294. }
  1295. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  1296. asml.remove(hp1);
  1297. hp1.free;
  1298. Result:=true;
  1299. exit;
  1300. end;
  1301. else
  1302. ;
  1303. end;
  1304. end
  1305. else if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  1306. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  1307. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  1308. then
  1309. begin
  1310. InputVal := debug_operstr(taicpu(p).oper[0]^);
  1311. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  1312. case taicpu(p).opsize of
  1313. S_B:
  1314. if (taicpu(hp1).oper[0]^.val = $ff) then
  1315. begin
  1316. { Convert:
  1317. movb x, %regl movb x, %regl
  1318. andw ffh, %regw andl ffh, %regd
  1319. To:
  1320. movzbw x, %regd movzbl x, %regd
  1321. (Identical registers, just different sizes)
  1322. }
  1323. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  1324. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  1325. case taicpu(hp1).opsize of
  1326. S_W: NewSize := S_BW;
  1327. S_L: NewSize := S_BL;
  1328. {$ifdef x86_64}
  1329. S_Q: NewSize := S_BQ;
  1330. {$endif x86_64}
  1331. else
  1332. InternalError(2018011510);
  1333. end;
  1334. end
  1335. else
  1336. NewSize := S_NO;
  1337. S_W:
  1338. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1339. begin
  1340. { Convert:
  1341. movw x, %regw
  1342. andl ffffh, %regd
  1343. To:
  1344. movzwl x, %regd
  1345. (Identical registers, just different sizes)
  1346. }
  1347. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  1348. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  1349. case taicpu(hp1).opsize of
  1350. S_L: NewSize := S_WL;
  1351. {$ifdef x86_64}
  1352. S_Q: NewSize := S_WQ;
  1353. {$endif x86_64}
  1354. else
  1355. InternalError(2018011511);
  1356. end;
  1357. end
  1358. else
  1359. NewSize := S_NO;
  1360. else
  1361. NewSize := S_NO;
  1362. end;
  1363. if NewSize <> S_NO then
  1364. begin
  1365. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  1366. { The actual optimization }
  1367. taicpu(p).opcode := A_MOVZX;
  1368. taicpu(p).changeopsize(NewSize);
  1369. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  1370. { Safeguard if "and" is followed by a conditional command }
  1371. TransferUsedRegs(TmpUsedRegs);
  1372. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  1373. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  1374. begin
  1375. { At this point, the "and" command is effectively equivalent to
  1376. "test %reg,%reg". This will be handled separately by the
  1377. Peephole Optimizer. [Kit] }
  1378. DebugMsg(SPeepholeOptimization + PreMessage +
  1379. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1380. end
  1381. else
  1382. begin
  1383. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  1384. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1385. asml.Remove(hp1);
  1386. hp1.Free;
  1387. end;
  1388. Result := True;
  1389. Exit;
  1390. end;
  1391. end;
  1392. end;
  1393. { Next instruction is also a MOV ? }
  1394. if GetNextInstruction_p and
  1395. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  1396. begin
  1397. if (taicpu(p).oper[1]^.typ = top_reg) and
  1398. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1399. begin
  1400. TransferUsedRegs(TmpUsedRegs);
  1401. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1402. { we have
  1403. mov x, %treg
  1404. mov %treg, y
  1405. }
  1406. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^)) and
  1407. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  1408. { we've got
  1409. mov x, %treg
  1410. mov %treg, y
  1411. with %treg is not used after }
  1412. case taicpu(p).oper[0]^.typ Of
  1413. top_reg:
  1414. begin
  1415. { change
  1416. mov %reg, %treg
  1417. mov %treg, y
  1418. to
  1419. mov %reg, y
  1420. }
  1421. if taicpu(hp1).oper[1]^.typ=top_reg then
  1422. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1423. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1424. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 2 done',p);
  1425. asml.remove(hp1);
  1426. hp1.free;
  1427. Result:=true;
  1428. Exit;
  1429. end;
  1430. top_const:
  1431. begin
  1432. { change
  1433. mov const, %treg
  1434. mov %treg, y
  1435. to
  1436. mov const, y
  1437. }
  1438. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  1439. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1440. begin
  1441. if taicpu(hp1).oper[1]^.typ=top_reg then
  1442. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1443. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1444. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  1445. asml.remove(hp1);
  1446. hp1.free;
  1447. Result:=true;
  1448. Exit;
  1449. end;
  1450. end;
  1451. top_ref:
  1452. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  1453. begin
  1454. { change
  1455. mov mem, %treg
  1456. mov %treg, %reg
  1457. to
  1458. mov mem, %reg"
  1459. }
  1460. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1461. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  1462. asml.remove(hp1);
  1463. hp1.free;
  1464. Result:=true;
  1465. Exit;
  1466. end;
  1467. else
  1468. ;
  1469. end;
  1470. end;
  1471. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  1472. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  1473. { mov reg1, mem1 or mov mem1, reg1
  1474. mov mem2, reg2 mov reg2, mem2}
  1475. begin
  1476. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1477. { mov reg1, mem1 or mov mem1, reg1
  1478. mov mem2, reg1 mov reg2, mem1}
  1479. begin
  1480. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1481. { Removes the second statement from
  1482. mov reg1, mem1/reg2
  1483. mov mem1/reg2, reg1 }
  1484. begin
  1485. if taicpu(p).oper[0]^.typ=top_reg then
  1486. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1487. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  1488. asml.remove(hp1);
  1489. hp1.free;
  1490. Result:=true;
  1491. exit;
  1492. end
  1493. else
  1494. begin
  1495. TransferUsedRegs(TmpUsedRegs);
  1496. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1497. if (taicpu(p).oper[1]^.typ = top_ref) and
  1498. { mov reg1, mem1
  1499. mov mem2, reg1 }
  1500. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  1501. GetNextInstruction(hp1, hp2) and
  1502. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  1503. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  1504. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  1505. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  1506. { change to
  1507. mov reg1, mem1 mov reg1, mem1
  1508. mov mem2, reg1 cmp reg1, mem2
  1509. cmp mem1, reg1
  1510. }
  1511. begin
  1512. asml.remove(hp2);
  1513. hp2.free;
  1514. taicpu(hp1).opcode := A_CMP;
  1515. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  1516. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1517. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1518. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  1519. end;
  1520. end;
  1521. end
  1522. else if (taicpu(p).oper[1]^.typ=top_ref) and
  1523. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1524. begin
  1525. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1526. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1527. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  1528. end
  1529. else
  1530. begin
  1531. TransferUsedRegs(TmpUsedRegs);
  1532. if GetNextInstruction(hp1, hp2) and
  1533. MatchOpType(taicpu(p),top_ref,top_reg) and
  1534. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1535. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1536. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  1537. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  1538. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1539. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  1540. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  1541. { mov mem1, %reg1
  1542. mov %reg1, mem2
  1543. mov mem2, reg2
  1544. to:
  1545. mov mem1, reg2
  1546. mov reg2, mem2}
  1547. begin
  1548. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  1549. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  1550. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  1551. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1552. asml.remove(hp2);
  1553. hp2.free;
  1554. end
  1555. {$ifdef i386}
  1556. { this is enabled for i386 only, as the rules to create the reg sets below
  1557. are too complicated for x86-64, so this makes this code too error prone
  1558. on x86-64
  1559. }
  1560. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  1561. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  1562. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  1563. { mov mem1, reg1 mov mem1, reg1
  1564. mov reg1, mem2 mov reg1, mem2
  1565. mov mem2, reg2 mov mem2, reg1
  1566. to: to:
  1567. mov mem1, reg1 mov mem1, reg1
  1568. mov mem1, reg2 mov reg1, mem2
  1569. mov reg1, mem2
  1570. or (if mem1 depends on reg1
  1571. and/or if mem2 depends on reg2)
  1572. to:
  1573. mov mem1, reg1
  1574. mov reg1, mem2
  1575. mov reg1, reg2
  1576. }
  1577. begin
  1578. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  1579. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  1580. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  1581. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  1582. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1583. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1584. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1585. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  1586. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  1587. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1588. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  1589. end
  1590. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  1591. begin
  1592. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  1593. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1594. end
  1595. else
  1596. begin
  1597. asml.remove(hp2);
  1598. hp2.free;
  1599. end
  1600. {$endif i386}
  1601. ;
  1602. end;
  1603. end;
  1604. (* { movl [mem1],reg1
  1605. movl [mem1],reg2
  1606. to
  1607. movl [mem1],reg1
  1608. movl reg1,reg2
  1609. }
  1610. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1611. (taicpu(p).oper[1]^.typ = top_reg) and
  1612. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1613. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1614. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1615. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  1616. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  1617. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  1618. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg)
  1619. else*)
  1620. { movl const1,[mem1]
  1621. movl [mem1],reg1
  1622. to
  1623. movl const1,reg1
  1624. movl reg1,[mem1]
  1625. }
  1626. if MatchOpType(Taicpu(p),top_const,top_ref) and
  1627. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  1628. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1629. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  1630. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  1631. begin
  1632. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1633. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  1634. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  1635. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  1636. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  1637. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  1638. Result:=true;
  1639. exit;
  1640. end;
  1641. {
  1642. mov* x,reg1
  1643. mov* y,reg1
  1644. to
  1645. mov* y,reg1
  1646. }
  1647. if (taicpu(p).oper[1]^.typ=top_reg) and
  1648. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  1649. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^)) then
  1650. begin
  1651. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 4 done',p);
  1652. { take care of the register (de)allocs following p }
  1653. UpdateUsedRegs(tai(p.next));
  1654. asml.remove(p);
  1655. p.free;
  1656. p:=hp1;
  1657. Result:=true;
  1658. exit;
  1659. end;
  1660. end;
  1661. { search further than the next instruction for a mov }
  1662. if (cs_opt_level3 in current_settings.optimizerswitches) and
  1663. { check as much as possible before the expensive GetNextInstructionUsingReg call }
  1664. (taicpu(p).oper[1]^.typ = top_reg) and
  1665. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  1666. { we work with hp2 here, so hp1 can be still used later on when
  1667. checking for GetNextInstruction_p }
  1668. GetNextInstructionUsingReg(p,hp2,taicpu(p).oper[1]^.reg) and
  1669. MatchInstruction(hp2,A_MOV,[]) and
  1670. MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  1671. ((taicpu(p).oper[0]^.typ=top_const) or
  1672. ((taicpu(p).oper[0]^.typ=top_reg) and
  1673. not(RegUsedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  1674. )
  1675. ) then
  1676. begin
  1677. TransferUsedRegs(TmpUsedRegs);
  1678. { we have
  1679. mov x, %treg
  1680. mov %treg, y
  1681. }
  1682. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp2).oper[1]^)) and
  1683. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs)) then
  1684. { we've got
  1685. mov x, %treg
  1686. mov %treg, y
  1687. with %treg is not used after }
  1688. case taicpu(p).oper[0]^.typ Of
  1689. top_reg:
  1690. begin
  1691. { change
  1692. mov %reg, %treg
  1693. mov %treg, y
  1694. to
  1695. mov %reg, y
  1696. }
  1697. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp2,usedregs);
  1698. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  1699. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  1700. { take care of the register (de)allocs following p }
  1701. UpdateUsedRegs(tai(p.next));
  1702. asml.remove(p);
  1703. p.free;
  1704. p:=hp1;
  1705. Result:=true;
  1706. Exit;
  1707. end;
  1708. top_const:
  1709. begin
  1710. { change
  1711. mov const, %treg
  1712. mov %treg, y
  1713. to
  1714. mov const, y
  1715. }
  1716. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  1717. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1718. begin
  1719. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  1720. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  1721. { take care of the register (de)allocs following p }
  1722. UpdateUsedRegs(tai(p.next));
  1723. asml.remove(p);
  1724. p.free;
  1725. p:=hp1;
  1726. Result:=true;
  1727. Exit;
  1728. end;
  1729. end;
  1730. else
  1731. Internalerror(2019103001);
  1732. end;
  1733. end;
  1734. { Change
  1735. mov %reg1, %reg2
  1736. xxx %reg2, ???
  1737. to
  1738. mov %reg1, %reg2
  1739. xxx %reg1, ???
  1740. to avoid a write/read penalty
  1741. }
  1742. if GetNextInstruction_p and
  1743. MatchOpType(taicpu(p),top_reg,top_reg) and
  1744. MatchInstruction(hp1,A_OR,A_AND,A_TEST,[]) and
  1745. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1746. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1747. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  1748. { we have
  1749. mov %reg1, %reg2
  1750. test/or/and %reg2, %reg2
  1751. }
  1752. begin
  1753. TransferUsedRegs(TmpUsedRegs);
  1754. { reg1 will be used after the first instruction,
  1755. so update the allocation info }
  1756. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1757. if GetNextInstruction(hp1, hp2) and
  1758. (hp2.typ = ait_instruction) and
  1759. taicpu(hp2).is_jmp and
  1760. not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, hp1, TmpUsedRegs)) then
  1761. { change
  1762. mov %reg1, %reg2
  1763. test/or/and %reg2, %reg2
  1764. jxx
  1765. to
  1766. test %reg1, %reg1
  1767. jxx
  1768. }
  1769. begin
  1770. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1771. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1772. DebugMsg(SPeepholeOptimization + 'MovTestJxx2TestMov done',p);
  1773. asml.remove(p);
  1774. p.free;
  1775. p := hp1;
  1776. Exit;
  1777. end
  1778. else
  1779. { change
  1780. mov %reg1, %reg2
  1781. test/or/and %reg2, %reg2
  1782. to
  1783. mov %reg1, %reg2
  1784. test/or/and %reg1, %reg1
  1785. }
  1786. begin
  1787. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1788. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1789. DebugMsg(SPeepholeOptimization + 'MovTestJxx2MovTestJxx done',p);
  1790. end;
  1791. end;
  1792. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  1793. x >= RetOffset) as it doesn't do anything (it writes either to a
  1794. parameter or to the temporary storage room for the function
  1795. result)
  1796. }
  1797. if GetNextInstruction_p and
  1798. IsExitCode(hp1) and
  1799. MatchOpType(taicpu(p),top_reg,top_ref) and
  1800. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  1801. not(assigned(current_procinfo.procdef.funcretsym) and
  1802. (taicpu(p).oper[1]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1803. (taicpu(p).oper[1]^.ref^.index = NR_NO) then
  1804. begin
  1805. asml.remove(p);
  1806. p.free;
  1807. p:=hp1;
  1808. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  1809. RemoveLastDeallocForFuncRes(p);
  1810. Result:=true;
  1811. exit;
  1812. end;
  1813. if GetNextInstruction_p and
  1814. MatchOpType(taicpu(p),top_reg,top_ref) and
  1815. MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) and
  1816. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1817. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1818. begin
  1819. { change
  1820. mov reg1, mem1
  1821. test/cmp x, mem1
  1822. to
  1823. mov reg1, mem1
  1824. test/cmp x, reg1
  1825. }
  1826. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  1827. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  1828. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1829. exit;
  1830. end;
  1831. if GetNextInstruction_p and
  1832. (taicpu(p).oper[1]^.typ = top_reg) and
  1833. (hp1.typ = ait_instruction) and
  1834. GetNextInstruction(hp1, hp2) and
  1835. MatchInstruction(hp2,A_MOV,[]) and
  1836. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  1837. (IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg) or
  1838. ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  1839. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ)))
  1840. ) then
  1841. begin
  1842. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1843. (taicpu(hp2).oper[0]^.typ=top_reg) then
  1844. { change movsX/movzX reg/ref, reg2
  1845. add/sub/or/... reg3/$const, reg2
  1846. mov reg2 reg/ref
  1847. dealloc reg2
  1848. to
  1849. add/sub/or/... reg3/$const, reg/ref }
  1850. begin
  1851. TransferUsedRegs(TmpUsedRegs);
  1852. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1853. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1854. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1855. begin
  1856. { by example:
  1857. movswl %si,%eax movswl %si,%eax p
  1858. decl %eax addl %edx,%eax hp1
  1859. movw %ax,%si movw %ax,%si hp2
  1860. ->
  1861. movswl %si,%eax movswl %si,%eax p
  1862. decw %eax addw %edx,%eax hp1
  1863. movw %ax,%si movw %ax,%si hp2
  1864. }
  1865. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  1866. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1867. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1868. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize),p);
  1869. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1870. {
  1871. ->
  1872. movswl %si,%eax movswl %si,%eax p
  1873. decw %si addw %dx,%si hp1
  1874. movw %ax,%si movw %ax,%si hp2
  1875. }
  1876. case taicpu(hp1).ops of
  1877. 1:
  1878. begin
  1879. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1880. if taicpu(hp1).oper[0]^.typ=top_reg then
  1881. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1882. end;
  1883. 2:
  1884. begin
  1885. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1886. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1887. (taicpu(hp1).opcode<>A_SHL) and
  1888. (taicpu(hp1).opcode<>A_SHR) and
  1889. (taicpu(hp1).opcode<>A_SAR) then
  1890. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1891. end;
  1892. else
  1893. internalerror(2008042701);
  1894. end;
  1895. {
  1896. ->
  1897. decw %si addw %dx,%si p
  1898. }
  1899. asml.remove(p);
  1900. asml.remove(hp2);
  1901. p.Free;
  1902. hp2.Free;
  1903. p := hp1;
  1904. end;
  1905. end;
  1906. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1907. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  1908. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  1909. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  1910. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  1911. )
  1912. {$ifdef i386}
  1913. { byte registers of esi, edi, ebp, esp are not available on i386 }
  1914. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1915. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1916. {$endif i386}
  1917. then
  1918. { change movsX/movzX reg/ref, reg2
  1919. add/sub/or/... regX/$const, reg2
  1920. mov reg2, reg3
  1921. dealloc reg2
  1922. to
  1923. movsX/movzX reg/ref, reg3
  1924. add/sub/or/... reg3/$const, reg3
  1925. }
  1926. begin
  1927. TransferUsedRegs(TmpUsedRegs);
  1928. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1929. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1930. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1931. begin
  1932. { by example:
  1933. movswl %si,%eax movswl %si,%eax p
  1934. decl %eax addl %edx,%eax hp1
  1935. movw %ax,%si movw %ax,%si hp2
  1936. ->
  1937. movswl %si,%eax movswl %si,%eax p
  1938. decw %eax addw %edx,%eax hp1
  1939. movw %ax,%si movw %ax,%si hp2
  1940. }
  1941. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  1942. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1943. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1944. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  1945. { limit size of constants as well to avoid assembler errors, but
  1946. check opsize to avoid overflow when left shifting the 1 }
  1947. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=4) then
  1948. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl (topsize2memsize[taicpu(hp2).opsize]*8))-1);
  1949. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1950. taicpu(p).changeopsize(taicpu(hp2).opsize);
  1951. if taicpu(p).oper[0]^.typ=top_reg then
  1952. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1953. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  1954. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  1955. {
  1956. ->
  1957. movswl %si,%eax movswl %si,%eax p
  1958. decw %si addw %dx,%si hp1
  1959. movw %ax,%si movw %ax,%si hp2
  1960. }
  1961. case taicpu(hp1).ops of
  1962. 1:
  1963. begin
  1964. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1965. if taicpu(hp1).oper[0]^.typ=top_reg then
  1966. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1967. end;
  1968. 2:
  1969. begin
  1970. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1971. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1972. (taicpu(hp1).opcode<>A_SHL) and
  1973. (taicpu(hp1).opcode<>A_SHR) and
  1974. (taicpu(hp1).opcode<>A_SAR) then
  1975. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1976. end;
  1977. else
  1978. internalerror(2018111801);
  1979. end;
  1980. {
  1981. ->
  1982. decw %si addw %dx,%si p
  1983. }
  1984. asml.remove(hp2);
  1985. hp2.Free;
  1986. end;
  1987. end;
  1988. end;
  1989. if GetNextInstruction_p and
  1990. MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  1991. GetNextInstruction(hp1, hp2) and
  1992. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  1993. MatchOperand(Taicpu(p).oper[0]^,0) and
  1994. (Taicpu(p).oper[1]^.typ = top_reg) and
  1995. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  1996. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  1997. { mov reg1,0
  1998. bts reg1,operand1 --> mov reg1,operand2
  1999. or reg1,operand2 bts reg1,operand1}
  2000. begin
  2001. Taicpu(hp2).opcode:=A_MOV;
  2002. asml.remove(hp1);
  2003. insertllitem(hp2,hp2.next,hp1);
  2004. asml.remove(p);
  2005. p.free;
  2006. p:=hp1;
  2007. Result:=true;
  2008. exit;
  2009. end;
  2010. if GetNextInstruction_p and
  2011. MatchInstruction(hp1,A_LEA,[S_L]) and
  2012. MatchOpType(Taicpu(p),top_ref,top_reg) and
  2013. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  2014. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  2015. ) or
  2016. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  2017. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  2018. )
  2019. ) then
  2020. { mov reg1,ref
  2021. lea reg2,[reg1,reg2]
  2022. to
  2023. add reg2,ref}
  2024. begin
  2025. TransferUsedRegs(TmpUsedRegs);
  2026. { reg1 may not be used afterwards }
  2027. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  2028. begin
  2029. Taicpu(hp1).opcode:=A_ADD;
  2030. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  2031. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  2032. asml.remove(p);
  2033. p.free;
  2034. p:=hp1;
  2035. result:=true;
  2036. exit;
  2037. end;
  2038. end;
  2039. end;
  2040. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  2041. var
  2042. hp1 : tai;
  2043. begin
  2044. Result:=false;
  2045. if taicpu(p).ops <> 2 then
  2046. exit;
  2047. if GetNextInstruction(p,hp1) and
  2048. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  2049. (taicpu(hp1).ops = 2) then
  2050. begin
  2051. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2052. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2053. { movXX reg1, mem1 or movXX mem1, reg1
  2054. movXX mem2, reg2 movXX reg2, mem2}
  2055. begin
  2056. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2057. { movXX reg1, mem1 or movXX mem1, reg1
  2058. movXX mem2, reg1 movXX reg2, mem1}
  2059. begin
  2060. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2061. begin
  2062. { Removes the second statement from
  2063. movXX reg1, mem1/reg2
  2064. movXX mem1/reg2, reg1
  2065. }
  2066. if taicpu(p).oper[0]^.typ=top_reg then
  2067. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2068. { Removes the second statement from
  2069. movXX mem1/reg1, reg2
  2070. movXX reg2, mem1/reg1
  2071. }
  2072. if (taicpu(p).oper[1]^.typ=top_reg) and
  2073. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  2074. begin
  2075. asml.remove(p);
  2076. p.free;
  2077. GetNextInstruction(hp1,p);
  2078. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  2079. end
  2080. else
  2081. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  2082. asml.remove(hp1);
  2083. hp1.free;
  2084. Result:=true;
  2085. exit;
  2086. end
  2087. end;
  2088. end;
  2089. end;
  2090. end;
  2091. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  2092. var
  2093. hp1 : tai;
  2094. begin
  2095. result:=false;
  2096. { replace
  2097. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  2098. MovX %mreg2,%mreg1
  2099. dealloc %mreg2
  2100. by
  2101. <Op>X %mreg2,%mreg1
  2102. ?
  2103. }
  2104. if GetNextInstruction(p,hp1) and
  2105. { we mix single and double opperations here because we assume that the compiler
  2106. generates vmovapd only after double operations and vmovaps only after single operations }
  2107. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  2108. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2109. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  2110. (taicpu(p).oper[0]^.typ=top_reg) then
  2111. begin
  2112. TransferUsedRegs(TmpUsedRegs);
  2113. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2114. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2115. begin
  2116. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  2117. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2118. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  2119. asml.Remove(hp1);
  2120. hp1.Free;
  2121. result:=true;
  2122. end;
  2123. end;
  2124. end;
  2125. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  2126. var
  2127. hp1, hp2, hp3: tai;
  2128. l : ASizeInt;
  2129. begin
  2130. Result:=false;
  2131. { removes seg register prefixes from LEA operations, as they
  2132. don't do anything}
  2133. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  2134. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  2135. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2136. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  2137. { do not mess with leas acessing the stack pointer }
  2138. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  2139. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  2140. begin
  2141. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  2142. (taicpu(p).oper[0]^.ref^.offset = 0) then
  2143. begin
  2144. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  2145. taicpu(p).oper[1]^.reg);
  2146. InsertLLItem(p.previous,p.next, hp1);
  2147. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  2148. p.free;
  2149. p:=hp1;
  2150. Result:=true;
  2151. exit;
  2152. end
  2153. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  2154. begin
  2155. hp1:=taicpu(p.Next);
  2156. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  2157. asml.remove(p);
  2158. p.free;
  2159. p:=hp1;
  2160. Result:=true;
  2161. exit;
  2162. end
  2163. { continue to use lea to adjust the stack pointer,
  2164. it is the recommended way, but only if not optimizing for size }
  2165. else if (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  2166. (cs_opt_size in current_settings.optimizerswitches) then
  2167. with taicpu(p).oper[0]^.ref^ do
  2168. if (base = taicpu(p).oper[1]^.reg) then
  2169. begin
  2170. l:=offset;
  2171. if (l=1) and UseIncDec then
  2172. begin
  2173. taicpu(p).opcode:=A_INC;
  2174. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2175. taicpu(p).ops:=1;
  2176. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  2177. end
  2178. else if (l=-1) and UseIncDec then
  2179. begin
  2180. taicpu(p).opcode:=A_DEC;
  2181. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2182. taicpu(p).ops:=1;
  2183. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  2184. end
  2185. else
  2186. begin
  2187. if (l<0) and (l<>-2147483648) then
  2188. begin
  2189. taicpu(p).opcode:=A_SUB;
  2190. taicpu(p).loadConst(0,-l);
  2191. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  2192. end
  2193. else
  2194. begin
  2195. taicpu(p).opcode:=A_ADD;
  2196. taicpu(p).loadConst(0,l);
  2197. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  2198. end;
  2199. end;
  2200. Result:=true;
  2201. exit;
  2202. end;
  2203. end;
  2204. if GetNextInstruction(p,hp1) and
  2205. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  2206. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2207. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  2208. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  2209. begin
  2210. TransferUsedRegs(TmpUsedRegs);
  2211. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2212. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2213. begin
  2214. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2215. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  2216. asml.Remove(hp1);
  2217. hp1.Free;
  2218. result:=true;
  2219. end;
  2220. end;
  2221. { changes
  2222. lea offset1(regX), reg1
  2223. lea offset2(reg1), reg1
  2224. to
  2225. lea offset1+offset2(regX), reg1 }
  2226. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  2227. MatchInstruction(hp1,A_LEA,[S_L]) and
  2228. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  2229. (taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg) and
  2230. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2231. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2232. (taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2233. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2234. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2235. (taicpu(p).oper[0]^.ref^.index=taicpu(hp1).oper[0]^.ref^.index) and
  2236. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp1).oper[0]^.ref^.relsymbol) and
  2237. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp1).oper[0]^.ref^.scalefactor) and
  2238. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp1).oper[0]^.ref^.segment) and
  2239. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp1).oper[0]^.ref^.symbol) then
  2240. begin
  2241. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea done',p);
  2242. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  2243. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  2244. asml.Remove(p);
  2245. p.Free;
  2246. p:=hp1;
  2247. result:=true;
  2248. end;
  2249. { replace
  2250. lea x(stackpointer),stackpointer
  2251. call procname
  2252. lea -x(stackpointer),stackpointer
  2253. ret
  2254. by
  2255. jmp procname
  2256. this should never hurt except when pic is used, not sure
  2257. how to handle it then
  2258. but do it only on level 4 because it destroys stack back traces
  2259. }
  2260. if (cs_opt_level4 in current_settings.optimizerswitches) and
  2261. not(cs_create_pic in current_settings.moduleswitches) and
  2262. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2263. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  2264. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2265. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2266. (taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2267. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2268. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2269. GetNextInstruction(p, hp1) and
  2270. MatchInstruction(hp1,A_CALL,[S_NO]) and
  2271. GetNextInstruction(hp1, hp2) and
  2272. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  2273. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2274. (taicpu(p).oper[0]^.ref^.base=taicpu(hp2).oper[0]^.ref^.base) and
  2275. (taicpu(p).oper[0]^.ref^.index=taicpu(hp2).oper[0]^.ref^.index) and
  2276. (taicpu(p).oper[0]^.ref^.offset=-taicpu(hp2).oper[0]^.ref^.offset) and
  2277. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp2).oper[0]^.ref^.relsymbol) and
  2278. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp2).oper[0]^.ref^.scalefactor) and
  2279. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp2).oper[0]^.ref^.segment) and
  2280. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp2).oper[0]^.ref^.symbol) and
  2281. GetNextInstruction(hp2, hp3) and
  2282. MatchInstruction(hp3,A_RET,[S_NO]) and
  2283. (taicpu(hp3).ops=0) then
  2284. begin
  2285. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  2286. taicpu(hp1).opcode:=A_JMP;
  2287. taicpu(hp1).is_jmp:=true;
  2288. asml.remove(p);
  2289. asml.remove(hp2);
  2290. asml.remove(hp3);
  2291. p.free;
  2292. hp2.free;
  2293. hp3.free;
  2294. p:=hp1;
  2295. Result:=true;
  2296. end;
  2297. end;
  2298. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  2299. var
  2300. hp1 : tai;
  2301. begin
  2302. DoSubAddOpt := False;
  2303. if GetLastInstruction(p, hp1) and
  2304. (hp1.typ = ait_instruction) and
  2305. (taicpu(hp1).opsize = taicpu(p).opsize) then
  2306. case taicpu(hp1).opcode Of
  2307. A_DEC:
  2308. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  2309. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2310. begin
  2311. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  2312. asml.remove(hp1);
  2313. hp1.free;
  2314. end;
  2315. A_SUB:
  2316. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2317. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2318. begin
  2319. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  2320. asml.remove(hp1);
  2321. hp1.free;
  2322. end;
  2323. A_ADD:
  2324. begin
  2325. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2326. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2327. begin
  2328. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  2329. asml.remove(hp1);
  2330. hp1.free;
  2331. if (taicpu(p).oper[0]^.val = 0) then
  2332. begin
  2333. hp1 := tai(p.next);
  2334. asml.remove(p);
  2335. p.free;
  2336. if not GetLastInstruction(hp1, p) then
  2337. p := hp1;
  2338. DoSubAddOpt := True;
  2339. end
  2340. end;
  2341. end;
  2342. else
  2343. ;
  2344. end;
  2345. end;
  2346. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  2347. {$ifdef i386}
  2348. var
  2349. hp1 : tai;
  2350. {$endif i386}
  2351. begin
  2352. Result:=false;
  2353. { * change "subl $2, %esp; pushw x" to "pushl x"}
  2354. { * change "sub/add const1, reg" or "dec reg" followed by
  2355. "sub const2, reg" to one "sub ..., reg" }
  2356. if MatchOpType(taicpu(p),top_const,top_reg) then
  2357. begin
  2358. {$ifdef i386}
  2359. if (taicpu(p).oper[0]^.val = 2) and
  2360. (taicpu(p).oper[1]^.reg = NR_ESP) and
  2361. { Don't do the sub/push optimization if the sub }
  2362. { comes from setting up the stack frame (JM) }
  2363. (not(GetLastInstruction(p,hp1)) or
  2364. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  2365. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  2366. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  2367. begin
  2368. hp1 := tai(p.next);
  2369. while Assigned(hp1) and
  2370. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  2371. not RegReadByInstruction(NR_ESP,hp1) and
  2372. not RegModifiedByInstruction(NR_ESP,hp1) do
  2373. hp1 := tai(hp1.next);
  2374. if Assigned(hp1) and
  2375. MatchInstruction(hp1,A_PUSH,[S_W]) then
  2376. begin
  2377. taicpu(hp1).changeopsize(S_L);
  2378. if taicpu(hp1).oper[0]^.typ=top_reg then
  2379. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  2380. hp1 := tai(p.next);
  2381. asml.remove(p);
  2382. p.free;
  2383. p := hp1;
  2384. Result:=true;
  2385. exit;
  2386. end;
  2387. end;
  2388. {$endif i386}
  2389. if DoSubAddOpt(p) then
  2390. Result:=true;
  2391. end;
  2392. end;
  2393. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  2394. var
  2395. TmpBool1,TmpBool2 : Boolean;
  2396. tmpref : treference;
  2397. hp1,hp2: tai;
  2398. begin
  2399. Result:=false;
  2400. if MatchOpType(taicpu(p),top_const,top_reg) and
  2401. (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  2402. (taicpu(p).oper[0]^.val <= 3) then
  2403. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  2404. begin
  2405. { should we check the next instruction? }
  2406. TmpBool1 := True;
  2407. { have we found an add/sub which could be
  2408. integrated in the lea? }
  2409. TmpBool2 := False;
  2410. reference_reset(tmpref,2,[]);
  2411. TmpRef.index := taicpu(p).oper[1]^.reg;
  2412. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2413. while TmpBool1 and
  2414. GetNextInstruction(p, hp1) and
  2415. (tai(hp1).typ = ait_instruction) and
  2416. ((((taicpu(hp1).opcode = A_ADD) or
  2417. (taicpu(hp1).opcode = A_SUB)) and
  2418. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  2419. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  2420. (((taicpu(hp1).opcode = A_INC) or
  2421. (taicpu(hp1).opcode = A_DEC)) and
  2422. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2423. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg))) and
  2424. (not GetNextInstruction(hp1,hp2) or
  2425. not instrReadsFlags(hp2)) Do
  2426. begin
  2427. TmpBool1 := False;
  2428. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  2429. begin
  2430. TmpBool1 := True;
  2431. TmpBool2 := True;
  2432. case taicpu(hp1).opcode of
  2433. A_ADD:
  2434. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2435. A_SUB:
  2436. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2437. else
  2438. internalerror(2019050536);
  2439. end;
  2440. asml.remove(hp1);
  2441. hp1.free;
  2442. end
  2443. else
  2444. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2445. (((taicpu(hp1).opcode = A_ADD) and
  2446. (TmpRef.base = NR_NO)) or
  2447. (taicpu(hp1).opcode = A_INC) or
  2448. (taicpu(hp1).opcode = A_DEC)) then
  2449. begin
  2450. TmpBool1 := True;
  2451. TmpBool2 := True;
  2452. case taicpu(hp1).opcode of
  2453. A_ADD:
  2454. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  2455. A_INC:
  2456. inc(TmpRef.offset);
  2457. A_DEC:
  2458. dec(TmpRef.offset);
  2459. else
  2460. internalerror(2019050535);
  2461. end;
  2462. asml.remove(hp1);
  2463. hp1.free;
  2464. end;
  2465. end;
  2466. if TmpBool2
  2467. {$ifndef x86_64}
  2468. or
  2469. ((current_settings.optimizecputype < cpu_Pentium2) and
  2470. (taicpu(p).oper[0]^.val <= 3) and
  2471. not(cs_opt_size in current_settings.optimizerswitches))
  2472. {$endif x86_64}
  2473. then
  2474. begin
  2475. if not(TmpBool2) and
  2476. (taicpu(p).oper[0]^.val = 1) then
  2477. begin
  2478. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2479. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  2480. end
  2481. else
  2482. hp1 := taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  2483. taicpu(p).oper[1]^.reg);
  2484. InsertLLItem(p.previous, p.next, hp1);
  2485. p.free;
  2486. p := hp1;
  2487. end;
  2488. end
  2489. {$ifndef x86_64}
  2490. else if (current_settings.optimizecputype < cpu_Pentium2) and
  2491. MatchOpType(taicpu(p),top_const,top_reg) then
  2492. begin
  2493. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  2494. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  2495. (unlike shl, which is only Tairable in the U pipe) }
  2496. if taicpu(p).oper[0]^.val=1 then
  2497. begin
  2498. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2499. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  2500. InsertLLItem(p.previous, p.next, hp1);
  2501. p.free;
  2502. p := hp1;
  2503. end
  2504. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  2505. "shl $3, %reg" to "lea (,%reg,8), %reg }
  2506. else if (taicpu(p).opsize = S_L) and
  2507. (taicpu(p).oper[0]^.val<= 3) then
  2508. begin
  2509. reference_reset(tmpref,2,[]);
  2510. TmpRef.index := taicpu(p).oper[1]^.reg;
  2511. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2512. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  2513. InsertLLItem(p.previous, p.next, hp1);
  2514. p.free;
  2515. p := hp1;
  2516. end;
  2517. end
  2518. {$endif x86_64}
  2519. ;
  2520. end;
  2521. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  2522. var
  2523. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  2524. begin
  2525. Result:=false;
  2526. if MatchOpType(taicpu(p),top_reg) and
  2527. GetNextInstruction(p, hp1) and
  2528. MatchInstruction(hp1, A_TEST, [S_B]) and
  2529. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2530. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  2531. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  2532. GetNextInstruction(hp1, hp2) and
  2533. MatchInstruction(hp2, A_Jcc, []) then
  2534. { Change from: To:
  2535. set(C) %reg j(~C) label
  2536. test %reg,%reg
  2537. je label
  2538. set(C) %reg j(C) label
  2539. test %reg,%reg
  2540. jne label
  2541. }
  2542. begin
  2543. next := tai(p.Next);
  2544. TransferUsedRegs(TmpUsedRegs);
  2545. UpdateUsedRegs(TmpUsedRegs, next);
  2546. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2547. asml.Remove(hp1);
  2548. hp1.Free;
  2549. JumpC := taicpu(hp2).condition;
  2550. if conditions_equal(JumpC, C_E) then
  2551. SetC := inverse_cond(taicpu(p).condition)
  2552. else if conditions_equal(JumpC, C_NE) then
  2553. SetC := taicpu(p).condition
  2554. else
  2555. InternalError(2018061400);
  2556. if SetC = C_NONE then
  2557. InternalError(2018061401);
  2558. taicpu(hp2).SetCondition(SetC);
  2559. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  2560. begin
  2561. asml.Remove(p);
  2562. UpdateUsedRegs(next);
  2563. p.Free;
  2564. Result := True;
  2565. p := hp2;
  2566. end;
  2567. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  2568. end;
  2569. end;
  2570. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  2571. { returns true if a "continue" should be done after this optimization }
  2572. var
  2573. hp1, hp2: tai;
  2574. begin
  2575. Result := false;
  2576. if MatchOpType(taicpu(p),top_ref) and
  2577. GetNextInstruction(p, hp1) and
  2578. (hp1.typ = ait_instruction) and
  2579. (((taicpu(hp1).opcode = A_FLD) and
  2580. (taicpu(p).opcode = A_FSTP)) or
  2581. ((taicpu(p).opcode = A_FISTP) and
  2582. (taicpu(hp1).opcode = A_FILD))) and
  2583. MatchOpType(taicpu(hp1),top_ref) and
  2584. (taicpu(hp1).opsize = taicpu(p).opsize) and
  2585. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2586. begin
  2587. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  2588. if (taicpu(p).opsize=S_FX) and
  2589. GetNextInstruction(hp1, hp2) and
  2590. (hp2.typ = ait_instruction) and
  2591. IsExitCode(hp2) and
  2592. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  2593. not(assigned(current_procinfo.procdef.funcretsym) and
  2594. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  2595. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  2596. begin
  2597. asml.remove(p);
  2598. asml.remove(hp1);
  2599. p.free;
  2600. hp1.free;
  2601. p := hp2;
  2602. RemoveLastDeallocForFuncRes(p);
  2603. Result := true;
  2604. end
  2605. (* can't be done because the store operation rounds
  2606. else
  2607. { fst can't store an extended value! }
  2608. if (taicpu(p).opsize <> S_FX) and
  2609. (taicpu(p).opsize <> S_IQ) then
  2610. begin
  2611. if (taicpu(p).opcode = A_FSTP) then
  2612. taicpu(p).opcode := A_FST
  2613. else taicpu(p).opcode := A_FIST;
  2614. asml.remove(hp1);
  2615. hp1.free;
  2616. end
  2617. *)
  2618. end;
  2619. end;
  2620. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  2621. var
  2622. hp1, hp2: tai;
  2623. begin
  2624. result:=false;
  2625. if MatchOpType(taicpu(p),top_reg) and
  2626. GetNextInstruction(p, hp1) and
  2627. (hp1.typ = Ait_Instruction) and
  2628. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2629. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  2630. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  2631. { change to
  2632. fld reg fxxx reg,st
  2633. fxxxp st, st1 (hp1)
  2634. Remark: non commutative operations must be reversed!
  2635. }
  2636. begin
  2637. case taicpu(hp1).opcode Of
  2638. A_FMULP,A_FADDP,
  2639. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2640. begin
  2641. case taicpu(hp1).opcode Of
  2642. A_FADDP: taicpu(hp1).opcode := A_FADD;
  2643. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  2644. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  2645. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  2646. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  2647. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  2648. else
  2649. internalerror(2019050534);
  2650. end;
  2651. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2652. taicpu(hp1).oper[1]^.reg := NR_ST;
  2653. asml.remove(p);
  2654. p.free;
  2655. p := hp1;
  2656. Result:=true;
  2657. exit;
  2658. end;
  2659. else
  2660. ;
  2661. end;
  2662. end
  2663. else
  2664. if MatchOpType(taicpu(p),top_ref) and
  2665. GetNextInstruction(p, hp2) and
  2666. (hp2.typ = Ait_Instruction) and
  2667. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2668. (taicpu(p).opsize in [S_FS, S_FL]) and
  2669. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  2670. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  2671. if GetLastInstruction(p, hp1) and
  2672. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  2673. MatchOpType(taicpu(hp1),top_ref) and
  2674. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2675. if ((taicpu(hp2).opcode = A_FMULP) or
  2676. (taicpu(hp2).opcode = A_FADDP)) then
  2677. { change to
  2678. fld/fst mem1 (hp1) fld/fst mem1
  2679. fld mem1 (p) fadd/
  2680. faddp/ fmul st, st
  2681. fmulp st, st1 (hp2) }
  2682. begin
  2683. asml.remove(p);
  2684. p.free;
  2685. p := hp1;
  2686. if (taicpu(hp2).opcode = A_FADDP) then
  2687. taicpu(hp2).opcode := A_FADD
  2688. else
  2689. taicpu(hp2).opcode := A_FMUL;
  2690. taicpu(hp2).oper[1]^.reg := NR_ST;
  2691. end
  2692. else
  2693. { change to
  2694. fld/fst mem1 (hp1) fld/fst mem1
  2695. fld mem1 (p) fld st}
  2696. begin
  2697. taicpu(p).changeopsize(S_FL);
  2698. taicpu(p).loadreg(0,NR_ST);
  2699. end
  2700. else
  2701. begin
  2702. case taicpu(hp2).opcode Of
  2703. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2704. { change to
  2705. fld/fst mem1 (hp1) fld/fst mem1
  2706. fld mem2 (p) fxxx mem2
  2707. fxxxp st, st1 (hp2) }
  2708. begin
  2709. case taicpu(hp2).opcode Of
  2710. A_FADDP: taicpu(p).opcode := A_FADD;
  2711. A_FMULP: taicpu(p).opcode := A_FMUL;
  2712. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  2713. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  2714. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  2715. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  2716. else
  2717. internalerror(2019050533);
  2718. end;
  2719. asml.remove(hp2);
  2720. hp2.free;
  2721. end
  2722. else
  2723. ;
  2724. end
  2725. end
  2726. end;
  2727. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  2728. var
  2729. hp1,hp2: tai;
  2730. {$ifdef x86_64}
  2731. hp3: tai;
  2732. {$endif x86_64}
  2733. begin
  2734. Result:=false;
  2735. if MatchOpType(taicpu(p),top_reg,top_reg) and
  2736. GetNextInstruction(p, hp1) and
  2737. {$ifdef x86_64}
  2738. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  2739. {$else x86_64}
  2740. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  2741. {$endif x86_64}
  2742. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2743. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  2744. { mov reg1, reg2 mov reg1, reg2
  2745. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  2746. begin
  2747. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2748. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  2749. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  2750. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  2751. TransferUsedRegs(TmpUsedRegs);
  2752. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2753. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  2754. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  2755. then
  2756. begin
  2757. asml.remove(p);
  2758. p.free;
  2759. p := hp1;
  2760. Result:=true;
  2761. end;
  2762. exit;
  2763. end
  2764. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  2765. GetNextInstruction(p, hp1) and
  2766. {$ifdef x86_64}
  2767. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  2768. {$else x86_64}
  2769. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  2770. {$endif x86_64}
  2771. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2772. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  2773. or
  2774. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  2775. ) and
  2776. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  2777. { mov reg1, reg2
  2778. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  2779. begin
  2780. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  2781. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  2782. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  2783. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  2784. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  2785. asml.remove(p);
  2786. p.free;
  2787. p := hp1;
  2788. Result:=true;
  2789. exit;
  2790. end
  2791. else if (taicpu(p).oper[0]^.typ = top_ref) and
  2792. GetNextInstruction(p,hp1) and
  2793. (hp1.typ = ait_instruction) and
  2794. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  2795. doing it separately in both branches allows to do the cheap checks
  2796. with low probability earlier }
  2797. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2798. GetNextInstruction(hp1,hp2) and
  2799. MatchInstruction(hp2,A_MOV,[])
  2800. ) or
  2801. ((taicpu(hp1).opcode=A_LEA) and
  2802. GetNextInstruction(hp1,hp2) and
  2803. MatchInstruction(hp2,A_MOV,[]) and
  2804. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  2805. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  2806. ) or
  2807. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  2808. taicpu(p).oper[1]^.reg) and
  2809. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  2810. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  2811. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  2812. ) and
  2813. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  2814. )
  2815. ) and
  2816. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  2817. (taicpu(hp2).oper[1]^.typ = top_ref) then
  2818. begin
  2819. TransferUsedRegs(TmpUsedRegs);
  2820. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2821. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  2822. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  2823. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  2824. { change mov (ref), reg
  2825. add/sub/or/... reg2/$const, reg
  2826. mov reg, (ref)
  2827. # release reg
  2828. to add/sub/or/... reg2/$const, (ref) }
  2829. begin
  2830. case taicpu(hp1).opcode of
  2831. A_INC,A_DEC,A_NOT,A_NEG :
  2832. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2833. A_LEA :
  2834. begin
  2835. taicpu(hp1).opcode:=A_ADD;
  2836. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  2837. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  2838. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  2839. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  2840. else
  2841. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  2842. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2843. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  2844. end
  2845. else
  2846. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2847. end;
  2848. asml.remove(p);
  2849. asml.remove(hp2);
  2850. p.free;
  2851. hp2.free;
  2852. p := hp1
  2853. end;
  2854. Exit;
  2855. {$ifdef x86_64}
  2856. end
  2857. else if (taicpu(p).opsize = S_L) and
  2858. (taicpu(p).oper[1]^.typ = top_reg) and
  2859. (
  2860. GetNextInstruction(p, hp1) and
  2861. MatchInstruction(hp1, A_MOV,[]) and
  2862. (taicpu(hp1).opsize = S_L) and
  2863. (taicpu(hp1).oper[1]^.typ = top_reg)
  2864. ) and (
  2865. GetNextInstruction(hp1, hp2) and
  2866. (tai(hp2).typ=ait_instruction) and
  2867. (taicpu(hp2).opsize = S_Q) and
  2868. (
  2869. (
  2870. MatchInstruction(hp2, A_ADD,[]) and
  2871. (taicpu(hp2).opsize = S_Q) and
  2872. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2873. (
  2874. (
  2875. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2876. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2877. ) or (
  2878. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2879. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2880. )
  2881. )
  2882. ) or (
  2883. MatchInstruction(hp2, A_LEA,[]) and
  2884. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  2885. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  2886. (
  2887. (
  2888. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2889. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2890. ) or (
  2891. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2892. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  2893. )
  2894. ) and (
  2895. (
  2896. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2897. ) or (
  2898. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2899. )
  2900. )
  2901. )
  2902. )
  2903. ) and (
  2904. GetNextInstruction(hp2, hp3) and
  2905. MatchInstruction(hp3, A_SHR,[]) and
  2906. (taicpu(hp3).opsize = S_Q) and
  2907. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2908. (taicpu(hp3).oper[0]^.val = 1) and
  2909. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  2910. ) then
  2911. begin
  2912. { Change movl x, reg1d movl x, reg1d
  2913. movl y, reg2d movl y, reg2d
  2914. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  2915. shrq $1, reg1q shrq $1, reg1q
  2916. ( reg1d and reg2d can be switched around in the first two instructions )
  2917. To movl x, reg1d
  2918. addl y, reg1d
  2919. rcrl $1, reg1d
  2920. This corresponds to the common expression (x + y) shr 1, where
  2921. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  2922. smaller code, but won't account for x + y causing an overflow). [Kit]
  2923. }
  2924. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  2925. { Change first MOV command to have the same register as the final output }
  2926. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  2927. else
  2928. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  2929. { Change second MOV command to an ADD command. This is easier than
  2930. converting the existing command because it means we don't have to
  2931. touch 'y', which might be a complicated reference, and also the
  2932. fact that the third command might either be ADD or LEA. [Kit] }
  2933. taicpu(hp1).opcode := A_ADD;
  2934. { Delete old ADD/LEA instruction }
  2935. asml.remove(hp2);
  2936. hp2.free;
  2937. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  2938. taicpu(hp3).opcode := A_RCR;
  2939. taicpu(hp3).changeopsize(S_L);
  2940. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  2941. {$endif x86_64}
  2942. end;
  2943. end;
  2944. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  2945. var
  2946. hp1 : tai;
  2947. begin
  2948. Result:=false;
  2949. if (taicpu(p).ops >= 2) and
  2950. ((taicpu(p).oper[0]^.typ = top_const) or
  2951. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  2952. (taicpu(p).oper[1]^.typ = top_reg) and
  2953. ((taicpu(p).ops = 2) or
  2954. ((taicpu(p).oper[2]^.typ = top_reg) and
  2955. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  2956. GetLastInstruction(p,hp1) and
  2957. MatchInstruction(hp1,A_MOV,[]) and
  2958. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2959. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) or
  2960. ((taicpu(hp1).opsize=S_L) and (taicpu(p).opsize=S_Q) and SuperRegistersEqual(taicpu(hp1).oper[1]^.reg,taicpu(p).oper[1]^.reg))) then
  2961. begin
  2962. TransferUsedRegs(TmpUsedRegs);
  2963. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) then
  2964. { change
  2965. mov reg1,reg2
  2966. imul y,reg2 to imul y,reg1,reg2 }
  2967. begin
  2968. taicpu(p).ops := 3;
  2969. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2970. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  2971. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  2972. asml.remove(hp1);
  2973. hp1.free;
  2974. result:=true;
  2975. end;
  2976. end;
  2977. end;
  2978. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  2979. var
  2980. hp1 : tai;
  2981. begin
  2982. {
  2983. change
  2984. jmp .L1
  2985. ...
  2986. .L1:
  2987. ret
  2988. into
  2989. ret
  2990. }
  2991. result:=false;
  2992. if (taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full) and (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  2993. (taicpu(p).oper[0]^.ref^.index=NR_NO) then
  2994. begin
  2995. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  2996. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and
  2997. MatchInstruction(hp1,A_RET,[S_NO]) then
  2998. begin
  2999. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  3000. taicpu(p).opcode:=A_RET;
  3001. taicpu(p).is_jmp:=false;
  3002. taicpu(p).ops:=taicpu(hp1).ops;
  3003. case taicpu(hp1).ops of
  3004. 0:
  3005. taicpu(p).clearop(0);
  3006. 1:
  3007. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  3008. else
  3009. internalerror(2016041301);
  3010. end;
  3011. result:=true;
  3012. end;
  3013. end;
  3014. end;
  3015. function CanBeCMOV(p : tai) : boolean;
  3016. begin
  3017. CanBeCMOV:=assigned(p) and
  3018. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  3019. { we can't use cmov ref,reg because
  3020. ref could be nil and cmov still throws an exception
  3021. if ref=nil but the mov isn't done (FK)
  3022. or ((taicpu(p).oper[0]^.typ = top_ref) and
  3023. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  3024. }
  3025. (MatchOpType(taicpu(p),top_reg,top_reg) or
  3026. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  3027. it is not expected that this can cause a seg. violation }
  3028. (MatchOpType(taicpu(p),top_ref,top_reg) and
  3029. (((taicpu(p).oper[0]^.ref^.base=NR_NO) and (taicpu(p).oper[0]^.ref^.refaddr=addr_no)){$ifdef x86_64} or
  3030. ((taicpu(p).oper[0]^.ref^.base=NR_RIP) and (taicpu(p).oper[0]^.ref^.refaddr=addr_pic)){$endif x86_64}
  3031. ) and
  3032. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  3033. (taicpu(p).oper[0]^.ref^.offset=0)
  3034. )
  3035. );
  3036. end;
  3037. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  3038. var
  3039. hp1,hp2,hp3,hp4,hpmov2: tai;
  3040. carryadd_opcode : TAsmOp;
  3041. l : Longint;
  3042. condition : TAsmCond;
  3043. symbol: TAsmSymbol;
  3044. begin
  3045. result:=false;
  3046. symbol:=nil;
  3047. if GetNextInstruction(p,hp1) then
  3048. begin
  3049. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  3050. if (hp1.typ=ait_instruction) and
  3051. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  3052. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  3053. { jb @@1 cmc
  3054. inc/dec operand --> adc/sbb operand,0
  3055. @@1:
  3056. ... and ...
  3057. jnb @@1
  3058. inc/dec operand --> adc/sbb operand,0
  3059. @@1: }
  3060. begin
  3061. carryadd_opcode:=A_NONE;
  3062. if Taicpu(p).condition in [C_NAE,C_B] then
  3063. begin
  3064. if Taicpu(hp1).opcode=A_INC then
  3065. carryadd_opcode:=A_ADC;
  3066. if Taicpu(hp1).opcode=A_DEC then
  3067. carryadd_opcode:=A_SBB;
  3068. if carryadd_opcode<>A_NONE then
  3069. begin
  3070. Taicpu(p).clearop(0);
  3071. Taicpu(p).ops:=0;
  3072. Taicpu(p).is_jmp:=false;
  3073. Taicpu(p).opcode:=A_CMC;
  3074. Taicpu(p).condition:=C_NONE;
  3075. Taicpu(hp1).ops:=2;
  3076. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3077. Taicpu(hp1).loadconst(0,0);
  3078. Taicpu(hp1).opcode:=carryadd_opcode;
  3079. result:=true;
  3080. exit;
  3081. end;
  3082. end;
  3083. if Taicpu(p).condition in [C_AE,C_NB] then
  3084. begin
  3085. if Taicpu(hp1).opcode=A_INC then
  3086. carryadd_opcode:=A_ADC;
  3087. if Taicpu(hp1).opcode=A_DEC then
  3088. carryadd_opcode:=A_SBB;
  3089. if carryadd_opcode<>A_NONE then
  3090. begin
  3091. asml.remove(p);
  3092. p.free;
  3093. Taicpu(hp1).ops:=2;
  3094. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3095. Taicpu(hp1).loadconst(0,0);
  3096. Taicpu(hp1).opcode:=carryadd_opcode;
  3097. p:=hp1;
  3098. result:=true;
  3099. exit;
  3100. end;
  3101. end;
  3102. end;
  3103. if ((hp1.typ = ait_label) and (symbol = tai_label(hp1).labsym))
  3104. or ((hp1.typ = ait_align) and GetNextInstruction(hp1, hp2) and (hp2.typ = ait_label) and (symbol = tai_label(hp2).labsym)) then
  3105. begin
  3106. { If Jcc is immediately followed by the label that it's supposed to jump to, remove it }
  3107. DebugMsg(SPeepholeOptimization + 'Removed conditional jump whose destination was immediately after it', p);
  3108. UpdateUsedRegs(hp1);
  3109. TAsmLabel(symbol).decrefs;
  3110. { if the label refs. reach zero, remove any alignment before the label }
  3111. if (hp1.typ = ait_align) then
  3112. begin
  3113. UpdateUsedRegs(hp2);
  3114. if (TAsmLabel(symbol).getrefs = 0) then
  3115. begin
  3116. asml.Remove(hp1);
  3117. hp1.Free;
  3118. end;
  3119. hp1 := hp2; { Set hp1 to the label }
  3120. end;
  3121. asml.remove(p);
  3122. p.free;
  3123. if (TAsmLabel(symbol).getrefs = 0) then
  3124. begin
  3125. GetNextInstruction(hp1, p); { Instruction following the label }
  3126. asml.remove(hp1);
  3127. hp1.free;
  3128. UpdateUsedRegs(p);
  3129. Result := True;
  3130. end
  3131. else
  3132. begin
  3133. { We don't need to set the result to True because we know hp1
  3134. is a label and won't trigger any optimisation routines. [Kit] }
  3135. p := hp1;
  3136. end;
  3137. Exit;
  3138. end;
  3139. end;
  3140. {$ifndef i8086}
  3141. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  3142. begin
  3143. { check for
  3144. jCC xxx
  3145. <several movs>
  3146. xxx:
  3147. }
  3148. l:=0;
  3149. GetNextInstruction(p, hp1);
  3150. while assigned(hp1) and
  3151. CanBeCMOV(hp1) and
  3152. { stop on labels }
  3153. not(hp1.typ=ait_label) do
  3154. begin
  3155. inc(l);
  3156. GetNextInstruction(hp1,hp1);
  3157. end;
  3158. if assigned(hp1) then
  3159. begin
  3160. if FindLabel(tasmlabel(symbol),hp1) then
  3161. begin
  3162. if (l<=4) and (l>0) then
  3163. begin
  3164. condition:=inverse_cond(taicpu(p).condition);
  3165. GetNextInstruction(p,hp1);
  3166. repeat
  3167. if not Assigned(hp1) then
  3168. InternalError(2018062900);
  3169. taicpu(hp1).opcode:=A_CMOVcc;
  3170. taicpu(hp1).condition:=condition;
  3171. UpdateUsedRegs(hp1);
  3172. GetNextInstruction(hp1,hp1);
  3173. until not(CanBeCMOV(hp1));
  3174. { Don't decrement the reference count on the label yet, otherwise
  3175. GetNextInstruction might skip over the label if it drops to
  3176. zero. }
  3177. GetNextInstruction(hp1,hp2);
  3178. { if the label refs. reach zero, remove any alignment before the label }
  3179. if (hp1.typ = ait_align) and (hp2.typ = ait_label) then
  3180. begin
  3181. { Ref = 1 means it will drop to zero }
  3182. if (tasmlabel(symbol).getrefs=1) then
  3183. begin
  3184. asml.Remove(hp1);
  3185. hp1.Free;
  3186. end;
  3187. end
  3188. else
  3189. hp2 := hp1;
  3190. if not Assigned(hp2) then
  3191. InternalError(2018062910);
  3192. if (hp2.typ <> ait_label) then
  3193. begin
  3194. { There's something other than CMOVs here. Move the original jump
  3195. to right before this point, then break out.
  3196. Originally this was part of the above internal error, but it got
  3197. triggered on the bootstrapping process sometimes. Investigate. [Kit] }
  3198. asml.remove(p);
  3199. asml.insertbefore(p, hp2);
  3200. DebugMsg('Jcc/CMOVcc drop-out', p);
  3201. UpdateUsedRegs(p);
  3202. Result := True;
  3203. Exit;
  3204. end;
  3205. { Now we can safely decrement the reference count }
  3206. tasmlabel(symbol).decrefs;
  3207. { Remove the original jump }
  3208. asml.Remove(p);
  3209. p.Free;
  3210. GetNextInstruction(hp2, p); { Instruction after the label }
  3211. { Remove the label if this is its final reference }
  3212. if (tasmlabel(symbol).getrefs=0) then
  3213. begin
  3214. asml.remove(hp2);
  3215. hp2.free;
  3216. end;
  3217. if Assigned(p) then
  3218. begin
  3219. UpdateUsedRegs(p);
  3220. result:=true;
  3221. end;
  3222. exit;
  3223. end;
  3224. end
  3225. else
  3226. begin
  3227. { check further for
  3228. jCC xxx
  3229. <several movs 1>
  3230. jmp yyy
  3231. xxx:
  3232. <several movs 2>
  3233. yyy:
  3234. }
  3235. { hp2 points to jmp yyy }
  3236. hp2:=hp1;
  3237. { skip hp1 to xxx (or an align right before it) }
  3238. GetNextInstruction(hp1, hp1);
  3239. if assigned(hp2) and
  3240. assigned(hp1) and
  3241. (l<=3) and
  3242. (hp2.typ=ait_instruction) and
  3243. (taicpu(hp2).is_jmp) and
  3244. (taicpu(hp2).condition=C_None) and
  3245. { real label and jump, no further references to the
  3246. label are allowed }
  3247. (tasmlabel(symbol).getrefs=1) and
  3248. FindLabel(tasmlabel(symbol),hp1) then
  3249. begin
  3250. l:=0;
  3251. { skip hp1 to <several moves 2> }
  3252. if (hp1.typ = ait_align) then
  3253. GetNextInstruction(hp1, hp1);
  3254. GetNextInstruction(hp1, hpmov2);
  3255. hp1 := hpmov2;
  3256. while assigned(hp1) and
  3257. CanBeCMOV(hp1) do
  3258. begin
  3259. inc(l);
  3260. GetNextInstruction(hp1, hp1);
  3261. end;
  3262. { hp1 points to yyy (or an align right before it) }
  3263. hp3 := hp1;
  3264. if assigned(hp1) and
  3265. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  3266. begin
  3267. condition:=inverse_cond(taicpu(p).condition);
  3268. GetNextInstruction(p,hp1);
  3269. repeat
  3270. taicpu(hp1).opcode:=A_CMOVcc;
  3271. taicpu(hp1).condition:=condition;
  3272. UpdateUsedRegs(hp1);
  3273. GetNextInstruction(hp1,hp1);
  3274. until not(assigned(hp1)) or
  3275. not(CanBeCMOV(hp1));
  3276. condition:=inverse_cond(condition);
  3277. hp1 := hpmov2;
  3278. { hp1 is now at <several movs 2> }
  3279. while Assigned(hp1) and CanBeCMOV(hp1) do
  3280. begin
  3281. taicpu(hp1).opcode:=A_CMOVcc;
  3282. taicpu(hp1).condition:=condition;
  3283. UpdateUsedRegs(hp1);
  3284. GetNextInstruction(hp1,hp1);
  3285. end;
  3286. hp1 := p;
  3287. { Get first instruction after label }
  3288. GetNextInstruction(hp3, p);
  3289. if assigned(p) and (hp3.typ = ait_align) then
  3290. GetNextInstruction(p, p);
  3291. { Don't dereference yet, as doing so will cause
  3292. GetNextInstruction to skip the label and
  3293. optional align marker. [Kit] }
  3294. GetNextInstruction(hp2, hp4);
  3295. { remove jCC }
  3296. asml.remove(hp1);
  3297. hp1.free;
  3298. { Remove label xxx (it will have a ref of zero due to the initial check }
  3299. if (hp4.typ = ait_align) then
  3300. begin
  3301. { Account for alignment as well }
  3302. GetNextInstruction(hp4, hp1);
  3303. asml.remove(hp1);
  3304. hp1.free;
  3305. end;
  3306. asml.remove(hp4);
  3307. hp4.free;
  3308. { Now we can safely decrement it }
  3309. tasmlabel(symbol).decrefs;
  3310. { remove jmp }
  3311. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  3312. asml.remove(hp2);
  3313. hp2.free;
  3314. { Remove label yyy (and the optional alignment) if its reference will fall to zero }
  3315. if tasmlabel(symbol).getrefs = 1 then
  3316. begin
  3317. if (hp3.typ = ait_align) then
  3318. begin
  3319. { Account for alignment as well }
  3320. GetNextInstruction(hp3, hp1);
  3321. asml.remove(hp1);
  3322. hp1.free;
  3323. end;
  3324. asml.remove(hp3);
  3325. hp3.free;
  3326. { As before, now we can safely decrement it }
  3327. tasmlabel(symbol).decrefs;
  3328. end;
  3329. if Assigned(p) then
  3330. begin
  3331. UpdateUsedRegs(p);
  3332. result:=true;
  3333. end;
  3334. exit;
  3335. end;
  3336. end;
  3337. end;
  3338. end;
  3339. end;
  3340. {$endif i8086}
  3341. end;
  3342. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  3343. var
  3344. hp1,hp2: tai;
  3345. begin
  3346. result:=false;
  3347. if (taicpu(p).oper[1]^.typ = top_reg) and
  3348. GetNextInstruction(p,hp1) and
  3349. (hp1.typ = ait_instruction) and
  3350. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  3351. GetNextInstruction(hp1,hp2) and
  3352. MatchInstruction(hp2,A_MOV,[]) and
  3353. (taicpu(hp2).oper[0]^.typ = top_reg) and
  3354. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  3355. {$ifdef i386}
  3356. { not all registers have byte size sub registers on i386 }
  3357. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  3358. {$endif i386}
  3359. (((taicpu(hp1).ops=2) and
  3360. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  3361. ((taicpu(hp1).ops=1) and
  3362. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  3363. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  3364. begin
  3365. { change movsX/movzX reg/ref, reg2
  3366. add/sub/or/... reg3/$const, reg2
  3367. mov reg2 reg/ref
  3368. to add/sub/or/... reg3/$const, reg/ref }
  3369. { by example:
  3370. movswl %si,%eax movswl %si,%eax p
  3371. decl %eax addl %edx,%eax hp1
  3372. movw %ax,%si movw %ax,%si hp2
  3373. ->
  3374. movswl %si,%eax movswl %si,%eax p
  3375. decw %eax addw %edx,%eax hp1
  3376. movw %ax,%si movw %ax,%si hp2
  3377. }
  3378. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  3379. {
  3380. ->
  3381. movswl %si,%eax movswl %si,%eax p
  3382. decw %si addw %dx,%si hp1
  3383. movw %ax,%si movw %ax,%si hp2
  3384. }
  3385. case taicpu(hp1).ops of
  3386. 1:
  3387. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  3388. 2:
  3389. begin
  3390. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  3391. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  3392. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3393. end;
  3394. else
  3395. internalerror(2008042701);
  3396. end;
  3397. {
  3398. ->
  3399. decw %si addw %dx,%si p
  3400. }
  3401. DebugMsg(SPeepholeOptimization + 'var3',p);
  3402. asml.remove(p);
  3403. asml.remove(hp2);
  3404. p.free;
  3405. hp2.free;
  3406. p:=hp1;
  3407. end
  3408. else if taicpu(p).opcode=A_MOVZX then
  3409. begin
  3410. { removes superfluous And's after movzx's }
  3411. if (taicpu(p).oper[1]^.typ = top_reg) and
  3412. GetNextInstruction(p, hp1) and
  3413. (tai(hp1).typ = ait_instruction) and
  3414. (taicpu(hp1).opcode = A_AND) and
  3415. (taicpu(hp1).oper[0]^.typ = top_const) and
  3416. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3417. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3418. begin
  3419. case taicpu(p).opsize Of
  3420. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  3421. if (taicpu(hp1).oper[0]^.val = $ff) then
  3422. begin
  3423. DebugMsg(SPeepholeOptimization + 'var4',p);
  3424. asml.remove(hp1);
  3425. hp1.free;
  3426. end;
  3427. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  3428. if (taicpu(hp1).oper[0]^.val = $ffff) then
  3429. begin
  3430. DebugMsg(SPeepholeOptimization + 'var5',p);
  3431. asml.remove(hp1);
  3432. hp1.free;
  3433. end;
  3434. {$ifdef x86_64}
  3435. S_LQ:
  3436. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  3437. begin
  3438. if (cs_asm_source in current_settings.globalswitches) then
  3439. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  3440. asml.remove(hp1);
  3441. hp1.Free;
  3442. end;
  3443. {$endif x86_64}
  3444. else
  3445. ;
  3446. end;
  3447. end;
  3448. { changes some movzx constructs to faster synonims (all examples
  3449. are given with eax/ax, but are also valid for other registers)}
  3450. if (taicpu(p).oper[1]^.typ = top_reg) then
  3451. if (taicpu(p).oper[0]^.typ = top_reg) then
  3452. case taicpu(p).opsize of
  3453. S_BW:
  3454. begin
  3455. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3456. not(cs_opt_size in current_settings.optimizerswitches) then
  3457. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  3458. begin
  3459. taicpu(p).opcode := A_AND;
  3460. taicpu(p).changeopsize(S_W);
  3461. taicpu(p).loadConst(0,$ff);
  3462. DebugMsg(SPeepholeOptimization + 'var7',p);
  3463. end
  3464. else if GetNextInstruction(p, hp1) and
  3465. (tai(hp1).typ = ait_instruction) and
  3466. (taicpu(hp1).opcode = A_AND) and
  3467. (taicpu(hp1).oper[0]^.typ = top_const) and
  3468. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3469. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3470. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  3471. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  3472. begin
  3473. DebugMsg(SPeepholeOptimization + 'var8',p);
  3474. taicpu(p).opcode := A_MOV;
  3475. taicpu(p).changeopsize(S_W);
  3476. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  3477. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3478. end;
  3479. end;
  3480. S_BL:
  3481. begin
  3482. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3483. not(cs_opt_size in current_settings.optimizerswitches) then
  3484. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  3485. begin
  3486. taicpu(p).opcode := A_AND;
  3487. taicpu(p).changeopsize(S_L);
  3488. taicpu(p).loadConst(0,$ff)
  3489. end
  3490. else if GetNextInstruction(p, hp1) and
  3491. (tai(hp1).typ = ait_instruction) and
  3492. (taicpu(hp1).opcode = A_AND) and
  3493. (taicpu(hp1).oper[0]^.typ = top_const) and
  3494. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3495. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3496. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  3497. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  3498. begin
  3499. DebugMsg(SPeepholeOptimization + 'var10',p);
  3500. taicpu(p).opcode := A_MOV;
  3501. taicpu(p).changeopsize(S_L);
  3502. { do not use R_SUBWHOLE
  3503. as movl %rdx,%eax
  3504. is invalid in assembler PM }
  3505. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3506. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3507. end
  3508. end;
  3509. {$ifndef i8086}
  3510. S_WL:
  3511. begin
  3512. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3513. not(cs_opt_size in current_settings.optimizerswitches) then
  3514. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  3515. begin
  3516. DebugMsg(SPeepholeOptimization + 'var11',p);
  3517. taicpu(p).opcode := A_AND;
  3518. taicpu(p).changeopsize(S_L);
  3519. taicpu(p).loadConst(0,$ffff);
  3520. end
  3521. else if GetNextInstruction(p, hp1) and
  3522. (tai(hp1).typ = ait_instruction) and
  3523. (taicpu(hp1).opcode = A_AND) and
  3524. (taicpu(hp1).oper[0]^.typ = top_const) and
  3525. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3526. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3527. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  3528. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  3529. begin
  3530. DebugMsg(SPeepholeOptimization + 'var12',p);
  3531. taicpu(p).opcode := A_MOV;
  3532. taicpu(p).changeopsize(S_L);
  3533. { do not use R_SUBWHOLE
  3534. as movl %rdx,%eax
  3535. is invalid in assembler PM }
  3536. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3537. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3538. end;
  3539. end;
  3540. {$endif i8086}
  3541. else
  3542. ;
  3543. end
  3544. else if (taicpu(p).oper[0]^.typ = top_ref) then
  3545. begin
  3546. if GetNextInstruction(p, hp1) and
  3547. (tai(hp1).typ = ait_instruction) and
  3548. (taicpu(hp1).opcode = A_AND) and
  3549. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3550. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3551. begin
  3552. //taicpu(p).opcode := A_MOV;
  3553. case taicpu(p).opsize Of
  3554. S_BL:
  3555. begin
  3556. DebugMsg(SPeepholeOptimization + 'var13',p);
  3557. taicpu(hp1).changeopsize(S_L);
  3558. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3559. end;
  3560. S_WL:
  3561. begin
  3562. DebugMsg(SPeepholeOptimization + 'var14',p);
  3563. taicpu(hp1).changeopsize(S_L);
  3564. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3565. end;
  3566. S_BW:
  3567. begin
  3568. DebugMsg(SPeepholeOptimization + 'var15',p);
  3569. taicpu(hp1).changeopsize(S_W);
  3570. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3571. end;
  3572. {$ifdef x86_64}
  3573. S_BQ:
  3574. begin
  3575. DebugMsg(SPeepholeOptimization + 'var16',p);
  3576. taicpu(hp1).changeopsize(S_Q);
  3577. taicpu(hp1).loadConst(
  3578. 0, taicpu(hp1).oper[0]^.val and $ff);
  3579. end;
  3580. S_WQ:
  3581. begin
  3582. DebugMsg(SPeepholeOptimization + 'var17',p);
  3583. taicpu(hp1).changeopsize(S_Q);
  3584. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  3585. end;
  3586. S_LQ:
  3587. begin
  3588. DebugMsg(SPeepholeOptimization + 'var18',p);
  3589. taicpu(hp1).changeopsize(S_Q);
  3590. taicpu(hp1).loadConst(
  3591. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  3592. end;
  3593. {$endif x86_64}
  3594. else
  3595. Internalerror(2017050704)
  3596. end;
  3597. end;
  3598. end;
  3599. end;
  3600. end;
  3601. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  3602. var
  3603. hp1 : tai;
  3604. MaskLength : Cardinal;
  3605. begin
  3606. Result:=false;
  3607. if GetNextInstruction(p, hp1) then
  3608. begin
  3609. if MatchOpType(taicpu(p),top_const,top_reg) and
  3610. MatchInstruction(hp1,A_AND,[]) and
  3611. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3612. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3613. { the second register must contain the first one, so compare their subreg types }
  3614. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  3615. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  3616. { change
  3617. and const1, reg
  3618. and const2, reg
  3619. to
  3620. and (const1 and const2), reg
  3621. }
  3622. begin
  3623. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  3624. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  3625. asml.remove(p);
  3626. p.Free;
  3627. p:=hp1;
  3628. Result:=true;
  3629. exit;
  3630. end
  3631. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3632. MatchInstruction(hp1,A_MOVZX,[]) and
  3633. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3634. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3635. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3636. (((taicpu(p).opsize=S_W) and
  3637. (taicpu(hp1).opsize=S_BW)) or
  3638. ((taicpu(p).opsize=S_L) and
  3639. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3640. {$ifdef x86_64}
  3641. or
  3642. ((taicpu(p).opsize=S_Q) and
  3643. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  3644. {$endif x86_64}
  3645. ) then
  3646. begin
  3647. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3648. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  3649. ) or
  3650. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3651. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  3652. then
  3653. begin
  3654. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  3655. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  3656. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  3657. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  3658. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  3659. }
  3660. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  3661. asml.remove(hp1);
  3662. hp1.free;
  3663. Exit;
  3664. end;
  3665. end
  3666. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3667. MatchInstruction(hp1,A_SHL,[]) and
  3668. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3669. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3670. begin
  3671. {$ifopt R+}
  3672. {$define RANGE_WAS_ON}
  3673. {$R-}
  3674. {$endif}
  3675. { get length of potential and mask }
  3676. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  3677. { really a mask? }
  3678. {$ifdef RANGE_WAS_ON}
  3679. {$R+}
  3680. {$endif}
  3681. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  3682. { unmasked part shifted out? }
  3683. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  3684. begin
  3685. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  3686. { take care of the register (de)allocs following p }
  3687. UpdateUsedRegs(tai(p.next));
  3688. asml.remove(p);
  3689. p.free;
  3690. p:=hp1;
  3691. Result:=true;
  3692. exit;
  3693. end;
  3694. end
  3695. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3696. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  3697. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3698. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3699. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3700. (((taicpu(p).opsize=S_W) and
  3701. (taicpu(hp1).opsize=S_BW)) or
  3702. ((taicpu(p).opsize=S_L) and
  3703. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3704. {$ifdef x86_64}
  3705. or
  3706. ((taicpu(p).opsize=S_Q) and
  3707. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  3708. {$endif x86_64}
  3709. ) then
  3710. begin
  3711. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3712. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  3713. ) or
  3714. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3715. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  3716. {$ifdef x86_64}
  3717. or
  3718. (((taicpu(hp1).opsize)=S_LQ) and
  3719. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  3720. )
  3721. {$endif x86_64}
  3722. then
  3723. begin
  3724. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  3725. asml.remove(hp1);
  3726. hp1.free;
  3727. Exit;
  3728. end;
  3729. end
  3730. else if (taicpu(p).oper[1]^.typ = top_reg) and
  3731. (hp1.typ = ait_instruction) and
  3732. (taicpu(hp1).is_jmp) and
  3733. (taicpu(hp1).opcode<>A_JMP) and
  3734. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  3735. begin
  3736. { change
  3737. and x, reg
  3738. jxx
  3739. to
  3740. test x, reg
  3741. jxx
  3742. if reg is deallocated before the
  3743. jump, but only if it's a conditional jump (PFV)
  3744. }
  3745. taicpu(p).opcode := A_TEST;
  3746. Exit;
  3747. end;
  3748. end;
  3749. { Lone AND tests }
  3750. if MatchOpType(taicpu(p),top_const,top_reg) then
  3751. begin
  3752. {
  3753. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  3754. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  3755. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  3756. }
  3757. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  3758. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  3759. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  3760. begin
  3761. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  3762. end;
  3763. end;
  3764. end;
  3765. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  3766. begin
  3767. Result:=false;
  3768. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3769. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  3770. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  3771. begin
  3772. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  3773. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  3774. taicpu(p).opcode:=A_ADD;
  3775. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  3776. result:=true;
  3777. end
  3778. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3779. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  3780. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  3781. begin
  3782. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  3783. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  3784. taicpu(p).opcode:=A_ADD;
  3785. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  3786. result:=true;
  3787. end;
  3788. end;
  3789. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  3790. var
  3791. Value, RegName: string;
  3792. begin
  3793. Result:=false;
  3794. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  3795. begin
  3796. case taicpu(p).oper[0]^.val of
  3797. 0:
  3798. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  3799. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  3800. begin
  3801. { change "mov $0,%reg" into "xor %reg,%reg" }
  3802. taicpu(p).opcode := A_XOR;
  3803. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  3804. Result := True;
  3805. end;
  3806. $1..$FFFFFFFF:
  3807. begin
  3808. { Code size reduction by J. Gareth "Kit" Moreton }
  3809. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  3810. case taicpu(p).opsize of
  3811. S_Q:
  3812. begin
  3813. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  3814. Value := debug_tostr(taicpu(p).oper[0]^.val);
  3815. { The actual optimization }
  3816. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3817. taicpu(p).changeopsize(S_L);
  3818. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  3819. Result := True;
  3820. end;
  3821. else
  3822. ;
  3823. end;
  3824. end;
  3825. end;
  3826. end;
  3827. end;
  3828. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  3829. begin
  3830. Result:=false;
  3831. { change "cmp $0, %reg" to "test %reg, %reg" }
  3832. if MatchOpType(taicpu(p),top_const,top_reg) and
  3833. (taicpu(p).oper[0]^.val = 0) then
  3834. begin
  3835. taicpu(p).opcode := A_TEST;
  3836. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3837. Result:=true;
  3838. end;
  3839. end;
  3840. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  3841. var
  3842. IsTestConstX : Boolean;
  3843. hp1,hp2 : tai;
  3844. begin
  3845. Result:=false;
  3846. { removes the line marked with (x) from the sequence
  3847. and/or/xor/add/sub/... $x, %y
  3848. test/or %y, %y | test $-1, %y (x)
  3849. j(n)z _Label
  3850. as the first instruction already adjusts the ZF
  3851. %y operand may also be a reference }
  3852. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  3853. MatchOperand(taicpu(p).oper[0]^,-1);
  3854. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  3855. GetLastInstruction(p, hp1) and
  3856. (tai(hp1).typ = ait_instruction) and
  3857. GetNextInstruction(p,hp2) and
  3858. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  3859. case taicpu(hp1).opcode Of
  3860. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  3861. begin
  3862. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3863. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3864. { and in case of carry for A(E)/B(E)/C/NC }
  3865. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  3866. ((taicpu(hp1).opcode <> A_ADD) and
  3867. (taicpu(hp1).opcode <> A_SUB))) then
  3868. begin
  3869. hp1 := tai(p.next);
  3870. asml.remove(p);
  3871. p.free;
  3872. p := tai(hp1);
  3873. Result:=true;
  3874. end;
  3875. end;
  3876. A_SHL, A_SAL, A_SHR, A_SAR:
  3877. begin
  3878. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3879. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  3880. { therefore, it's only safe to do this optimization for }
  3881. { shifts by a (nonzero) constant }
  3882. (taicpu(hp1).oper[0]^.typ = top_const) and
  3883. (taicpu(hp1).oper[0]^.val <> 0) and
  3884. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3885. { and in case of carry for A(E)/B(E)/C/NC }
  3886. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3887. begin
  3888. hp1 := tai(p.next);
  3889. asml.remove(p);
  3890. p.free;
  3891. p := tai(hp1);
  3892. Result:=true;
  3893. end;
  3894. end;
  3895. A_DEC, A_INC, A_NEG:
  3896. begin
  3897. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  3898. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3899. { and in case of carry for A(E)/B(E)/C/NC }
  3900. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3901. begin
  3902. case taicpu(hp1).opcode of
  3903. A_DEC, A_INC:
  3904. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  3905. begin
  3906. case taicpu(hp1).opcode Of
  3907. A_DEC: taicpu(hp1).opcode := A_SUB;
  3908. A_INC: taicpu(hp1).opcode := A_ADD;
  3909. else
  3910. ;
  3911. end;
  3912. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  3913. taicpu(hp1).loadConst(0,1);
  3914. taicpu(hp1).ops:=2;
  3915. end;
  3916. else
  3917. ;
  3918. end;
  3919. hp1 := tai(p.next);
  3920. asml.remove(p);
  3921. p.free;
  3922. p := tai(hp1);
  3923. Result:=true;
  3924. end;
  3925. end
  3926. else
  3927. { change "test $-1,%reg" into "test %reg,%reg" }
  3928. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3929. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3930. end { case }
  3931. { change "test $-1,%reg" into "test %reg,%reg" }
  3932. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3933. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3934. end;
  3935. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  3936. var
  3937. hp1 : tai;
  3938. {$ifndef x86_64}
  3939. hp2 : taicpu;
  3940. {$endif x86_64}
  3941. begin
  3942. Result:=false;
  3943. {$ifndef x86_64}
  3944. { don't do this on modern CPUs, this really hurts them due to
  3945. broken call/ret pairing }
  3946. if (current_settings.optimizecputype < cpu_Pentium2) and
  3947. not(cs_create_pic in current_settings.moduleswitches) and
  3948. GetNextInstruction(p, hp1) and
  3949. MatchInstruction(hp1,A_JMP,[S_NO]) and
  3950. MatchOpType(taicpu(hp1),top_ref) and
  3951. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  3952. begin
  3953. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  3954. InsertLLItem(p.previous, p, hp2);
  3955. taicpu(p).opcode := A_JMP;
  3956. taicpu(p).is_jmp := true;
  3957. asml.remove(hp1);
  3958. hp1.free;
  3959. Result:=true;
  3960. end
  3961. else
  3962. {$endif x86_64}
  3963. { replace
  3964. call procname
  3965. ret
  3966. by
  3967. jmp procname
  3968. but do it only on level 4 because it destroys stack back traces
  3969. }
  3970. if (cs_opt_level4 in current_settings.optimizerswitches) and
  3971. GetNextInstruction(p, hp1) and
  3972. MatchInstruction(hp1,A_RET,[S_NO]) and
  3973. (taicpu(hp1).ops=0) then
  3974. begin
  3975. taicpu(p).opcode := A_JMP;
  3976. taicpu(p).is_jmp := true;
  3977. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  3978. asml.remove(hp1);
  3979. hp1.free;
  3980. Result:=true;
  3981. end;
  3982. end;
  3983. {$ifdef x86_64}
  3984. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  3985. var
  3986. PreMessage: string;
  3987. begin
  3988. Result := False;
  3989. { Code size reduction by J. Gareth "Kit" Moreton }
  3990. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  3991. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  3992. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  3993. then
  3994. begin
  3995. { Has 64-bit register name and opcode suffix }
  3996. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  3997. { The actual optimization }
  3998. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3999. if taicpu(p).opsize = S_BQ then
  4000. taicpu(p).changeopsize(S_BL)
  4001. else
  4002. taicpu(p).changeopsize(S_WL);
  4003. DebugMsg(SPeepholeOptimization + PreMessage +
  4004. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  4005. end;
  4006. end;
  4007. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  4008. var
  4009. PreMessage, RegName: string;
  4010. begin
  4011. { Code size reduction by J. Gareth "Kit" Moreton }
  4012. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  4013. as this removes the REX prefix }
  4014. Result := False;
  4015. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  4016. Exit;
  4017. if taicpu(p).oper[0]^.typ <> top_reg then
  4018. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  4019. InternalError(2018011500);
  4020. case taicpu(p).opsize of
  4021. S_Q:
  4022. begin
  4023. if (getsupreg(taicpu(p).oper[0]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP]) then
  4024. begin
  4025. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  4026. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  4027. { The actual optimization }
  4028. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4029. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4030. taicpu(p).changeopsize(S_L);
  4031. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  4032. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  4033. end;
  4034. end;
  4035. else
  4036. ;
  4037. end;
  4038. end;
  4039. {$endif}
  4040. procedure TX86AsmOptimizer.OptReferences;
  4041. var
  4042. p: tai;
  4043. i: Integer;
  4044. begin
  4045. p := BlockStart;
  4046. while (p <> BlockEnd) Do
  4047. begin
  4048. if p.typ=ait_instruction then
  4049. begin
  4050. for i:=0 to taicpu(p).ops-1 do
  4051. if taicpu(p).oper[i]^.typ=top_ref then
  4052. optimize_ref(taicpu(p).oper[i]^.ref^,false);
  4053. end;
  4054. p:=tai(p.next);
  4055. end;
  4056. end;
  4057. end.