aoptx86.pas 151 KB

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