aoptx86.pas 159 KB

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