aoptx86.pas 166 KB

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