aoptx86.pas 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  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 aint($ffffffff));
  598. S_Q: taicpu(hp1).loadConst(0,l Xor aint($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 aint($ffffffff));
  616. S_Q: taicpu(p).loadConst(0,l Xor aint($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 aint($ffffffff));
  632. S_Q: taicpu(p).loadConst(0,l Xor aint($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(hp1.next));
  2218. if (RefsEqual(taicpu(hp2).oper[1]^.ref^, taicpu(p).oper[0]^.ref^) and
  2219. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2, TmpUsedRegs))) then
  2220. { change mov (ref), reg
  2221. add/sub/or/... reg2/$const, reg
  2222. mov reg, (ref)
  2223. # release reg
  2224. to add/sub/or/... reg2/$const, (ref) }
  2225. begin
  2226. case taicpu(hp1).opcode of
  2227. A_INC,A_DEC,A_NOT,A_NEG :
  2228. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2229. A_LEA :
  2230. begin
  2231. taicpu(hp1).opcode:=A_ADD;
  2232. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  2233. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  2234. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  2235. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  2236. else
  2237. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  2238. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2239. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  2240. end
  2241. else
  2242. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2243. end;
  2244. asml.remove(p);
  2245. asml.remove(hp2);
  2246. p.free;
  2247. hp2.free;
  2248. p := hp1
  2249. end;
  2250. ReleaseUsedRegs(TmpUsedRegs);
  2251. Exit;
  2252. {$ifdef x86_64}
  2253. end
  2254. else if (taicpu(p).opsize = S_L) and
  2255. (taicpu(p).oper[1]^.typ = top_reg) and
  2256. (
  2257. GetNextInstruction(p, hp1) and
  2258. MatchInstruction(hp1, A_MOV,[]) and
  2259. (taicpu(hp1).opsize = S_L) and
  2260. (taicpu(hp1).oper[1]^.typ = top_reg)
  2261. ) and (
  2262. GetNextInstruction(hp1, hp2) and
  2263. (taicpu(hp2).opsize = S_Q) and
  2264. (
  2265. (
  2266. MatchInstruction(hp2, A_ADD,[]) and
  2267. (taicpu(hp2).opsize = S_Q) and
  2268. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2269. (
  2270. (
  2271. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2272. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2273. ) or (
  2274. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2275. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2276. )
  2277. )
  2278. ) or (
  2279. MatchInstruction(hp2, A_LEA,[]) and
  2280. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  2281. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  2282. (
  2283. (
  2284. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2285. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2286. ) or (
  2287. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2288. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  2289. )
  2290. ) and (
  2291. (
  2292. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2293. ) or (
  2294. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2295. )
  2296. )
  2297. )
  2298. )
  2299. ) and (
  2300. GetNextInstruction(hp2, hp3) and
  2301. MatchInstruction(hp3, A_SHR,[]) and
  2302. (taicpu(hp3).opsize = S_Q) and
  2303. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2304. (taicpu(hp3).oper[0]^.val = 1) and
  2305. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  2306. ) then
  2307. begin
  2308. { Change movl x, reg1d movl x, reg1d
  2309. movl y, reg2d movl y, reg2d
  2310. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  2311. shrq $1, reg1q shrq $1, reg1q
  2312. ( reg1d and reg2d can be switched around in the first two instructions )
  2313. To movl x, reg1d
  2314. addl y, reg1d
  2315. rcrl $1, reg1d
  2316. This corresponds to the common expression (x + y) shr 1, where
  2317. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  2318. smaller code, but won't account for x + y causing an overflow). [Kit]
  2319. }
  2320. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  2321. { Change first MOV command to have the same register as the final output }
  2322. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  2323. else
  2324. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  2325. { Change second MOV command to an ADD command. This is easier than
  2326. converting the existing command because it means we don't have to
  2327. touch 'y', which might be a complicated reference, and also the
  2328. fact that the third command might either be ADD or LEA. [Kit] }
  2329. taicpu(hp1).opcode := A_ADD;
  2330. { Delete old ADD/LEA instruction }
  2331. asml.remove(hp2);
  2332. hp2.free;
  2333. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  2334. taicpu(hp3).opcode := A_RCR;
  2335. taicpu(hp3).changeopsize(S_L);
  2336. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  2337. {$endif x86_64}
  2338. end;
  2339. end;
  2340. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  2341. var
  2342. TmpUsedRegs : TAllUsedRegs;
  2343. hp1 : tai;
  2344. begin
  2345. Result:=false;
  2346. if (taicpu(p).ops >= 2) and
  2347. ((taicpu(p).oper[0]^.typ = top_const) or
  2348. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  2349. (taicpu(p).oper[1]^.typ = top_reg) and
  2350. ((taicpu(p).ops = 2) or
  2351. ((taicpu(p).oper[2]^.typ = top_reg) and
  2352. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  2353. GetLastInstruction(p,hp1) and
  2354. MatchInstruction(hp1,A_MOV,[]) and
  2355. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2356. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) or
  2357. ((taicpu(hp1).opsize=S_L) and (taicpu(p).opsize=S_Q) and SuperRegistersEqual(taicpu(hp1).oper[1]^.reg,taicpu(p).oper[1]^.reg))) then
  2358. begin
  2359. CopyUsedRegs(TmpUsedRegs);
  2360. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) then
  2361. { change
  2362. mov reg1,reg2
  2363. imul y,reg2 to imul y,reg1,reg2 }
  2364. begin
  2365. taicpu(p).ops := 3;
  2366. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2367. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  2368. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  2369. asml.remove(hp1);
  2370. hp1.free;
  2371. result:=true;
  2372. end;
  2373. ReleaseUsedRegs(TmpUsedRegs);
  2374. end;
  2375. end;
  2376. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  2377. var
  2378. hp1 : tai;
  2379. begin
  2380. {
  2381. change
  2382. jmp .L1
  2383. ...
  2384. .L1:
  2385. ret
  2386. into
  2387. ret
  2388. }
  2389. result:=false;
  2390. 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
  2391. (taicpu(p).oper[0]^.ref^.index=NR_NO) then
  2392. begin
  2393. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  2394. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and
  2395. MatchInstruction(hp1,A_RET,[S_NO]) then
  2396. begin
  2397. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  2398. taicpu(p).opcode:=A_RET;
  2399. taicpu(p).is_jmp:=false;
  2400. taicpu(p).ops:=taicpu(hp1).ops;
  2401. case taicpu(hp1).ops of
  2402. 0:
  2403. taicpu(p).clearop(0);
  2404. 1:
  2405. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  2406. else
  2407. internalerror(2016041301);
  2408. end;
  2409. result:=true;
  2410. end;
  2411. end;
  2412. end;
  2413. function CanBeCMOV(p : tai) : boolean;
  2414. begin
  2415. CanBeCMOV:=assigned(p) and
  2416. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  2417. { we can't use cmov ref,reg because
  2418. ref could be nil and cmov still throws an exception
  2419. if ref=nil but the mov isn't done (FK)
  2420. or ((taicpu(p).oper[0]^.typ = top_ref) and
  2421. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  2422. }
  2423. MatchOpType(taicpu(p),top_reg,top_reg);
  2424. end;
  2425. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  2426. var
  2427. hp1,hp2,hp3,hp4,hpmov2: tai;
  2428. carryadd_opcode : TAsmOp;
  2429. l : Longint;
  2430. condition : TAsmCond;
  2431. symbol: TAsmSymbol;
  2432. begin
  2433. result:=false;
  2434. symbol:=nil;
  2435. if GetNextInstruction(p,hp1) then
  2436. begin
  2437. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  2438. if (hp1.typ=ait_instruction) and
  2439. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  2440. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  2441. { jb @@1 cmc
  2442. inc/dec operand --> adc/sbb operand,0
  2443. @@1:
  2444. ... and ...
  2445. jnb @@1
  2446. inc/dec operand --> adc/sbb operand,0
  2447. @@1: }
  2448. begin
  2449. carryadd_opcode:=A_NONE;
  2450. if Taicpu(p).condition in [C_NAE,C_B] then
  2451. begin
  2452. if Taicpu(hp1).opcode=A_INC then
  2453. carryadd_opcode:=A_ADC;
  2454. if Taicpu(hp1).opcode=A_DEC then
  2455. carryadd_opcode:=A_SBB;
  2456. if carryadd_opcode<>A_NONE then
  2457. begin
  2458. Taicpu(p).clearop(0);
  2459. Taicpu(p).ops:=0;
  2460. Taicpu(p).is_jmp:=false;
  2461. Taicpu(p).opcode:=A_CMC;
  2462. Taicpu(p).condition:=C_NONE;
  2463. Taicpu(hp1).ops:=2;
  2464. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  2465. Taicpu(hp1).loadconst(0,0);
  2466. Taicpu(hp1).opcode:=carryadd_opcode;
  2467. result:=true;
  2468. exit;
  2469. end;
  2470. end;
  2471. if Taicpu(p).condition in [C_AE,C_NB] then
  2472. begin
  2473. if Taicpu(hp1).opcode=A_INC then
  2474. carryadd_opcode:=A_ADC;
  2475. if Taicpu(hp1).opcode=A_DEC then
  2476. carryadd_opcode:=A_SBB;
  2477. if carryadd_opcode<>A_NONE then
  2478. begin
  2479. asml.remove(p);
  2480. p.free;
  2481. Taicpu(hp1).ops:=2;
  2482. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  2483. Taicpu(hp1).loadconst(0,0);
  2484. Taicpu(hp1).opcode:=carryadd_opcode;
  2485. p:=hp1;
  2486. result:=true;
  2487. exit;
  2488. end;
  2489. end;
  2490. end;
  2491. if ((hp1.typ = ait_label) and (symbol = tai_label(hp1).labsym))
  2492. or ((hp1.typ = ait_align) and GetNextInstruction(hp1, hp2) and (hp2.typ = ait_label) and (symbol = tai_label(hp2).labsym)) then
  2493. begin
  2494. { If Jcc is immediately followed by the label that it's supposed to jump to, remove it }
  2495. DebugMsg(SPeepholeOptimization + 'Removed conditional jump whose destination was immediately after it', p);
  2496. UpdateUsedRegs(hp1);
  2497. TAsmLabel(symbol).decrefs;
  2498. { if the label refs. reach zero, remove any alignment before the label }
  2499. if (hp1.typ = ait_align) then
  2500. begin
  2501. UpdateUsedRegs(hp2);
  2502. if (TAsmLabel(symbol).getrefs = 0) then
  2503. begin
  2504. asml.Remove(hp1);
  2505. hp1.Free;
  2506. end;
  2507. hp1 := hp2; { Set hp1 to the label }
  2508. end;
  2509. asml.remove(p);
  2510. p.free;
  2511. if (TAsmLabel(symbol).getrefs = 0) then
  2512. begin
  2513. GetNextInstruction(hp1, p); { Instruction following the label }
  2514. asml.remove(hp1);
  2515. hp1.free;
  2516. UpdateUsedRegs(p);
  2517. Result := True;
  2518. end
  2519. else
  2520. begin
  2521. { We don't need to set the result to True because we know hp1
  2522. is a label and won't trigger any optimisation routines. [Kit] }
  2523. p := hp1;
  2524. end;
  2525. Exit;
  2526. end;
  2527. end;
  2528. {$ifndef i8086}
  2529. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  2530. begin
  2531. { check for
  2532. jCC xxx
  2533. <several movs>
  2534. xxx:
  2535. }
  2536. l:=0;
  2537. GetNextInstruction(p, hp1);
  2538. while assigned(hp1) and
  2539. CanBeCMOV(hp1) and
  2540. { stop on labels }
  2541. not(hp1.typ=ait_label) do
  2542. begin
  2543. inc(l);
  2544. GetNextInstruction(hp1,hp1);
  2545. end;
  2546. if assigned(hp1) then
  2547. begin
  2548. if FindLabel(tasmlabel(symbol),hp1) then
  2549. begin
  2550. if (l<=4) and (l>0) then
  2551. begin
  2552. condition:=inverse_cond(taicpu(p).condition);
  2553. GetNextInstruction(p,hp1);
  2554. repeat
  2555. if not Assigned(hp1) then
  2556. InternalError(2018062900);
  2557. taicpu(hp1).opcode:=A_CMOVcc;
  2558. taicpu(hp1).condition:=condition;
  2559. UpdateUsedRegs(hp1);
  2560. GetNextInstruction(hp1,hp1);
  2561. until not(CanBeCMOV(hp1));
  2562. { Don't decrement the reference count on the label yet, otherwise
  2563. GetNextInstruction might skip over the label if it drops to
  2564. zero. }
  2565. GetNextInstruction(hp1,hp2);
  2566. { if the label refs. reach zero, remove any alignment before the label }
  2567. if (hp1.typ = ait_align) and (hp2.typ = ait_label) then
  2568. begin
  2569. { Ref = 1 means it will drop to zero }
  2570. if (tasmlabel(symbol).getrefs=1) then
  2571. begin
  2572. asml.Remove(hp1);
  2573. hp1.Free;
  2574. end;
  2575. end
  2576. else
  2577. hp2 := hp1;
  2578. if not Assigned(hp2) then
  2579. InternalError(2018062910);
  2580. if (hp2.typ <> ait_label) then
  2581. begin
  2582. { There's something other than CMOVs here. Move the original jump
  2583. to right before this point, then break out.
  2584. Originally this was part of the above internal error, but it got
  2585. triggered on the bootstrapping process sometimes. Investigate. [Kit] }
  2586. asml.remove(p);
  2587. asml.insertbefore(p, hp2);
  2588. DebugMsg('Jcc/CMOVcc drop-out', p);
  2589. UpdateUsedRegs(p);
  2590. Result := True;
  2591. Exit;
  2592. end;
  2593. { Now we can safely decrement the reference count }
  2594. tasmlabel(symbol).decrefs;
  2595. { Remove the original jump }
  2596. asml.Remove(p);
  2597. p.Free;
  2598. GetNextInstruction(hp2, p); { Instruction after the label }
  2599. { Remove the label if this is its final reference }
  2600. if (tasmlabel(symbol).getrefs=0) then
  2601. begin
  2602. asml.remove(hp2);
  2603. hp2.free;
  2604. end;
  2605. if Assigned(p) then
  2606. begin
  2607. UpdateUsedRegs(p);
  2608. result:=true;
  2609. end;
  2610. exit;
  2611. end;
  2612. end
  2613. else
  2614. begin
  2615. { check further for
  2616. jCC xxx
  2617. <several movs 1>
  2618. jmp yyy
  2619. xxx:
  2620. <several movs 2>
  2621. yyy:
  2622. }
  2623. { hp2 points to jmp yyy }
  2624. hp2:=hp1;
  2625. { skip hp1 to xxx (or an align right before it) }
  2626. GetNextInstruction(hp1, hp1);
  2627. if assigned(hp2) and
  2628. assigned(hp1) and
  2629. (l<=3) and
  2630. (hp2.typ=ait_instruction) and
  2631. (taicpu(hp2).is_jmp) and
  2632. (taicpu(hp2).condition=C_None) and
  2633. { real label and jump, no further references to the
  2634. label are allowed }
  2635. (tasmlabel(symbol).getrefs=1) and
  2636. FindLabel(tasmlabel(symbol),hp1) then
  2637. begin
  2638. l:=0;
  2639. { skip hp1 to <several moves 2> }
  2640. if (hp1.typ = ait_align) then
  2641. GetNextInstruction(hp1, hp1);
  2642. GetNextInstruction(hp1, hpmov2);
  2643. hp1 := hpmov2;
  2644. while assigned(hp1) and
  2645. CanBeCMOV(hp1) do
  2646. begin
  2647. inc(l);
  2648. GetNextInstruction(hp1, hp1);
  2649. end;
  2650. { hp1 points to yyy (or an align right before it) }
  2651. hp3 := hp1;
  2652. if assigned(hp1) and
  2653. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2654. begin
  2655. condition:=inverse_cond(taicpu(p).condition);
  2656. GetNextInstruction(p,hp1);
  2657. repeat
  2658. taicpu(hp1).opcode:=A_CMOVcc;
  2659. taicpu(hp1).condition:=condition;
  2660. UpdateUsedRegs(hp1);
  2661. GetNextInstruction(hp1,hp1);
  2662. until not(assigned(hp1)) or
  2663. not(CanBeCMOV(hp1));
  2664. condition:=inverse_cond(condition);
  2665. hp1 := hpmov2;
  2666. { hp1 is now at <several movs 2> }
  2667. while Assigned(hp1) and CanBeCMOV(hp1) do
  2668. begin
  2669. taicpu(hp1).opcode:=A_CMOVcc;
  2670. taicpu(hp1).condition:=condition;
  2671. UpdateUsedRegs(hp1);
  2672. GetNextInstruction(hp1,hp1);
  2673. end;
  2674. hp1 := p;
  2675. { Get first instruction after label }
  2676. GetNextInstruction(hp3, p);
  2677. if assigned(p) and (hp3.typ = ait_align) then
  2678. GetNextInstruction(p, p);
  2679. { Don't dereference yet, as doing so will cause
  2680. GetNextInstruction to skip the label and
  2681. optional align marker. [Kit] }
  2682. GetNextInstruction(hp2, hp4);
  2683. { remove jCC }
  2684. asml.remove(hp1);
  2685. hp1.free;
  2686. { Remove label xxx (it will have a ref of zero due to the initial check }
  2687. if (hp4.typ = ait_align) then
  2688. begin
  2689. { Account for alignment as well }
  2690. GetNextInstruction(hp4, hp1);
  2691. asml.remove(hp1);
  2692. hp1.free;
  2693. end;
  2694. asml.remove(hp4);
  2695. hp4.free;
  2696. { Now we can safely decrement it }
  2697. tasmlabel(symbol).decrefs;
  2698. { remove jmp }
  2699. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  2700. asml.remove(hp2);
  2701. hp2.free;
  2702. { Remove label yyy (and the optional alignment) if its reference will fall to zero }
  2703. if tasmlabel(symbol).getrefs = 1 then
  2704. begin
  2705. if (hp3.typ = ait_align) then
  2706. begin
  2707. { Account for alignment as well }
  2708. GetNextInstruction(hp3, hp1);
  2709. asml.remove(hp1);
  2710. hp1.free;
  2711. end;
  2712. asml.remove(hp3);
  2713. hp3.free;
  2714. { As before, now we can safely decrement it }
  2715. tasmlabel(symbol).decrefs;
  2716. end;
  2717. if Assigned(p) then
  2718. begin
  2719. UpdateUsedRegs(p);
  2720. result:=true;
  2721. end;
  2722. exit;
  2723. end;
  2724. end;
  2725. end;
  2726. end;
  2727. end;
  2728. {$endif i8086}
  2729. end;
  2730. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  2731. var
  2732. hp1,hp2: tai;
  2733. begin
  2734. result:=false;
  2735. if (taicpu(p).oper[1]^.typ = top_reg) and
  2736. GetNextInstruction(p,hp1) and
  2737. (hp1.typ = ait_instruction) and
  2738. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2739. GetNextInstruction(hp1,hp2) and
  2740. MatchInstruction(hp2,A_MOV,[]) and
  2741. (taicpu(hp2).oper[0]^.typ = top_reg) and
  2742. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  2743. {$ifdef i386}
  2744. { not all registers have byte size sub registers on i386 }
  2745. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  2746. {$endif i386}
  2747. (((taicpu(hp1).ops=2) and
  2748. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  2749. ((taicpu(hp1).ops=1) and
  2750. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  2751. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  2752. begin
  2753. { change movsX/movzX reg/ref, reg2
  2754. add/sub/or/... reg3/$const, reg2
  2755. mov reg2 reg/ref
  2756. to add/sub/or/... reg3/$const, reg/ref }
  2757. { by example:
  2758. movswl %si,%eax movswl %si,%eax p
  2759. decl %eax addl %edx,%eax hp1
  2760. movw %ax,%si movw %ax,%si hp2
  2761. ->
  2762. movswl %si,%eax movswl %si,%eax p
  2763. decw %eax addw %edx,%eax hp1
  2764. movw %ax,%si movw %ax,%si hp2
  2765. }
  2766. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2767. {
  2768. ->
  2769. movswl %si,%eax movswl %si,%eax p
  2770. decw %si addw %dx,%si hp1
  2771. movw %ax,%si movw %ax,%si hp2
  2772. }
  2773. case taicpu(hp1).ops of
  2774. 1:
  2775. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  2776. 2:
  2777. begin
  2778. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  2779. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  2780. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2781. end;
  2782. else
  2783. internalerror(2008042701);
  2784. end;
  2785. {
  2786. ->
  2787. decw %si addw %dx,%si p
  2788. }
  2789. DebugMsg(SPeepholeOptimization + 'var3',p);
  2790. asml.remove(p);
  2791. asml.remove(hp2);
  2792. p.free;
  2793. hp2.free;
  2794. p:=hp1;
  2795. end
  2796. else if taicpu(p).opcode=A_MOVZX then
  2797. begin
  2798. { removes superfluous And's after movzx's }
  2799. if (taicpu(p).oper[1]^.typ = top_reg) and
  2800. GetNextInstruction(p, hp1) and
  2801. (tai(hp1).typ = ait_instruction) and
  2802. (taicpu(hp1).opcode = A_AND) and
  2803. (taicpu(hp1).oper[0]^.typ = top_const) and
  2804. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2805. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2806. begin
  2807. case taicpu(p).opsize Of
  2808. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  2809. if (taicpu(hp1).oper[0]^.val = $ff) then
  2810. begin
  2811. DebugMsg(SPeepholeOptimization + 'var4',p);
  2812. asml.remove(hp1);
  2813. hp1.free;
  2814. end;
  2815. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  2816. if (taicpu(hp1).oper[0]^.val = $ffff) then
  2817. begin
  2818. DebugMsg(SPeepholeOptimization + 'var5',p);
  2819. asml.remove(hp1);
  2820. hp1.free;
  2821. end;
  2822. {$ifdef x86_64}
  2823. S_LQ:
  2824. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  2825. begin
  2826. if (cs_asm_source in current_settings.globalswitches) then
  2827. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  2828. asml.remove(hp1);
  2829. hp1.Free;
  2830. end;
  2831. {$endif x86_64}
  2832. end;
  2833. end;
  2834. { changes some movzx constructs to faster synonims (all examples
  2835. are given with eax/ax, but are also valid for other registers)}
  2836. if (taicpu(p).oper[1]^.typ = top_reg) then
  2837. if (taicpu(p).oper[0]^.typ = top_reg) then
  2838. case taicpu(p).opsize of
  2839. S_BW:
  2840. begin
  2841. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  2842. not(cs_opt_size in current_settings.optimizerswitches) then
  2843. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  2844. begin
  2845. taicpu(p).opcode := A_AND;
  2846. taicpu(p).changeopsize(S_W);
  2847. taicpu(p).loadConst(0,$ff);
  2848. DebugMsg(SPeepholeOptimization + 'var7',p);
  2849. end
  2850. else if GetNextInstruction(p, hp1) and
  2851. (tai(hp1).typ = ait_instruction) and
  2852. (taicpu(hp1).opcode = A_AND) and
  2853. (taicpu(hp1).oper[0]^.typ = top_const) and
  2854. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2855. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2856. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  2857. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  2858. begin
  2859. DebugMsg(SPeepholeOptimization + 'var8',p);
  2860. taicpu(p).opcode := A_MOV;
  2861. taicpu(p).changeopsize(S_W);
  2862. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  2863. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  2864. end;
  2865. end;
  2866. S_BL:
  2867. begin
  2868. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  2869. not(cs_opt_size in current_settings.optimizerswitches) then
  2870. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  2871. begin
  2872. taicpu(p).opcode := A_AND;
  2873. taicpu(p).changeopsize(S_L);
  2874. taicpu(p).loadConst(0,$ff)
  2875. end
  2876. else if GetNextInstruction(p, hp1) and
  2877. (tai(hp1).typ = ait_instruction) and
  2878. (taicpu(hp1).opcode = A_AND) and
  2879. (taicpu(hp1).oper[0]^.typ = top_const) and
  2880. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2881. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2882. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  2883. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  2884. begin
  2885. DebugMsg(SPeepholeOptimization + 'var10',p);
  2886. taicpu(p).opcode := A_MOV;
  2887. taicpu(p).changeopsize(S_L);
  2888. { do not use R_SUBWHOLE
  2889. as movl %rdx,%eax
  2890. is invalid in assembler PM }
  2891. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  2892. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  2893. end
  2894. end;
  2895. {$ifndef i8086}
  2896. S_WL:
  2897. begin
  2898. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  2899. not(cs_opt_size in current_settings.optimizerswitches) then
  2900. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  2901. begin
  2902. DebugMsg(SPeepholeOptimization + 'var11',p);
  2903. taicpu(p).opcode := A_AND;
  2904. taicpu(p).changeopsize(S_L);
  2905. taicpu(p).loadConst(0,$ffff);
  2906. end
  2907. else if GetNextInstruction(p, hp1) and
  2908. (tai(hp1).typ = ait_instruction) and
  2909. (taicpu(hp1).opcode = A_AND) and
  2910. (taicpu(hp1).oper[0]^.typ = top_const) and
  2911. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2912. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2913. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  2914. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  2915. begin
  2916. DebugMsg(SPeepholeOptimization + 'var12',p);
  2917. taicpu(p).opcode := A_MOV;
  2918. taicpu(p).changeopsize(S_L);
  2919. { do not use R_SUBWHOLE
  2920. as movl %rdx,%eax
  2921. is invalid in assembler PM }
  2922. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  2923. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  2924. end;
  2925. end;
  2926. {$endif i8086}
  2927. end
  2928. else if (taicpu(p).oper[0]^.typ = top_ref) then
  2929. begin
  2930. if GetNextInstruction(p, hp1) and
  2931. (tai(hp1).typ = ait_instruction) and
  2932. (taicpu(hp1).opcode = A_AND) and
  2933. MatchOpType(taicpu(hp1),top_const,top_reg) and
  2934. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2935. begin
  2936. taicpu(p).opcode := A_MOV;
  2937. case taicpu(p).opsize Of
  2938. S_BL:
  2939. begin
  2940. DebugMsg(SPeepholeOptimization + 'var13',p);
  2941. taicpu(p).changeopsize(S_L);
  2942. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  2943. end;
  2944. S_WL:
  2945. begin
  2946. DebugMsg(SPeepholeOptimization + 'var14',p);
  2947. taicpu(p).changeopsize(S_L);
  2948. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  2949. end;
  2950. S_BW:
  2951. begin
  2952. DebugMsg(SPeepholeOptimization + 'var15',p);
  2953. taicpu(p).changeopsize(S_W);
  2954. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  2955. end;
  2956. {$ifdef x86_64}
  2957. S_BQ:
  2958. begin
  2959. DebugMsg(SPeepholeOptimization + 'var16',p);
  2960. taicpu(p).changeopsize(S_Q);
  2961. taicpu(hp1).loadConst(
  2962. 0, taicpu(hp1).oper[0]^.val and $ff);
  2963. end;
  2964. S_WQ:
  2965. begin
  2966. DebugMsg(SPeepholeOptimization + 'var17',p);
  2967. taicpu(p).changeopsize(S_Q);
  2968. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  2969. end;
  2970. S_LQ:
  2971. begin
  2972. DebugMsg(SPeepholeOptimization + 'var18',p);
  2973. taicpu(p).changeopsize(S_Q);
  2974. taicpu(hp1).loadConst(
  2975. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  2976. end;
  2977. {$endif x86_64}
  2978. else
  2979. Internalerror(2017050704)
  2980. end;
  2981. end;
  2982. end;
  2983. end;
  2984. end;
  2985. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  2986. var
  2987. hp1 : tai;
  2988. RegName1, RegName2: string;
  2989. MaskLength : Cardinal;
  2990. begin
  2991. Result:=false;
  2992. if GetNextInstruction(p, hp1) then
  2993. begin
  2994. if MatchOpType(taicpu(p),top_const,top_reg) and
  2995. MatchInstruction(hp1,A_AND,[]) and
  2996. MatchOpType(taicpu(hp1),top_const,top_reg) and
  2997. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2998. { the second register must contain the first one, so compare their subreg types }
  2999. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  3000. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  3001. { change
  3002. and const1, reg
  3003. and const2, reg
  3004. to
  3005. and (const1 and const2), reg
  3006. }
  3007. begin
  3008. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  3009. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  3010. asml.remove(p);
  3011. p.Free;
  3012. p:=hp1;
  3013. Result:=true;
  3014. exit;
  3015. end
  3016. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3017. MatchInstruction(hp1,A_MOVZX,[]) and
  3018. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3019. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3020. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3021. (((taicpu(p).opsize=S_W) and
  3022. (taicpu(hp1).opsize=S_BW)) or
  3023. ((taicpu(p).opsize=S_L) and
  3024. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3025. {$ifdef x86_64}
  3026. or
  3027. ((taicpu(p).opsize=S_Q) and
  3028. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  3029. {$endif x86_64}
  3030. ) then
  3031. begin
  3032. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3033. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  3034. ) or
  3035. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3036. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  3037. then
  3038. begin
  3039. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  3040. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  3041. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  3042. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  3043. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  3044. }
  3045. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  3046. asml.remove(hp1);
  3047. hp1.free;
  3048. Exit;
  3049. end;
  3050. end
  3051. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3052. MatchInstruction(hp1,A_SHL,[]) and
  3053. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3054. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3055. begin
  3056. { get length of potential and mask }
  3057. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  3058. { really a mask? }
  3059. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  3060. { unmasked part shifted out? }
  3061. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  3062. begin
  3063. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  3064. { take care of the register (de)allocs following p }
  3065. UpdateUsedRegs(tai(p.next));
  3066. asml.remove(p);
  3067. p.free;
  3068. p:=hp1;
  3069. Result:=true;
  3070. exit;
  3071. end;
  3072. end
  3073. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3074. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  3075. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3076. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3077. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3078. (((taicpu(p).opsize=S_W) and
  3079. (taicpu(hp1).opsize=S_BW)) or
  3080. ((taicpu(p).opsize=S_L) and
  3081. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3082. {$ifdef x86_64}
  3083. or
  3084. ((taicpu(p).opsize=S_Q) and
  3085. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  3086. {$endif x86_64}
  3087. ) then
  3088. begin
  3089. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3090. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  3091. ) or
  3092. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3093. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  3094. {$ifdef x86_64}
  3095. or
  3096. (((taicpu(hp1).opsize)=S_LQ) and
  3097. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  3098. )
  3099. {$endif x86_64}
  3100. then
  3101. begin
  3102. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  3103. asml.remove(hp1);
  3104. hp1.free;
  3105. Exit;
  3106. end;
  3107. end
  3108. else if (taicpu(p).oper[1]^.typ = top_reg) and
  3109. (hp1.typ = ait_instruction) and
  3110. (taicpu(hp1).is_jmp) and
  3111. (taicpu(hp1).opcode<>A_JMP) and
  3112. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  3113. begin
  3114. { change
  3115. and x, reg
  3116. jxx
  3117. to
  3118. test x, reg
  3119. jxx
  3120. if reg is deallocated before the
  3121. jump, but only if it's a conditional jump (PFV)
  3122. }
  3123. taicpu(p).opcode := A_TEST;
  3124. Exit;
  3125. end;
  3126. end;
  3127. { Lone AND tests }
  3128. if MatchOpType(taicpu(p),top_const,top_reg) then
  3129. begin
  3130. {
  3131. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  3132. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  3133. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  3134. }
  3135. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  3136. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  3137. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  3138. begin
  3139. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  3140. end;
  3141. end;
  3142. end;
  3143. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  3144. begin
  3145. Result:=false;
  3146. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3147. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  3148. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  3149. begin
  3150. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  3151. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  3152. taicpu(p).opcode:=A_ADD;
  3153. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  3154. result:=true;
  3155. end
  3156. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3157. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  3158. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  3159. begin
  3160. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  3161. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  3162. taicpu(p).opcode:=A_ADD;
  3163. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  3164. result:=true;
  3165. end;
  3166. end;
  3167. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  3168. var
  3169. Value, RegName: string;
  3170. begin
  3171. Result:=false;
  3172. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  3173. begin
  3174. case taicpu(p).oper[0]^.val of
  3175. 0:
  3176. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  3177. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  3178. begin
  3179. { change "mov $0,%reg" into "xor %reg,%reg" }
  3180. taicpu(p).opcode := A_XOR;
  3181. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  3182. Result := True;
  3183. end;
  3184. $1..$FFFFFFFF:
  3185. begin
  3186. { Code size reduction by J. Gareth "Kit" Moreton }
  3187. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  3188. case taicpu(p).opsize of
  3189. S_Q:
  3190. begin
  3191. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  3192. Value := debug_tostr(taicpu(p).oper[0]^.val);
  3193. { The actual optimization }
  3194. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3195. taicpu(p).changeopsize(S_L);
  3196. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  3197. Result := True;
  3198. end;
  3199. end;
  3200. end;
  3201. end;
  3202. end;
  3203. end;
  3204. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  3205. begin
  3206. Result:=false;
  3207. { change "cmp $0, %reg" to "test %reg, %reg" }
  3208. if MatchOpType(taicpu(p),top_const,top_reg) and
  3209. (taicpu(p).oper[0]^.val = 0) then
  3210. begin
  3211. taicpu(p).opcode := A_TEST;
  3212. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3213. Result:=true;
  3214. end;
  3215. end;
  3216. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  3217. var
  3218. IsTestConstX : Boolean;
  3219. hp1,hp2 : tai;
  3220. begin
  3221. Result:=false;
  3222. { removes the line marked with (x) from the sequence
  3223. and/or/xor/add/sub/... $x, %y
  3224. test/or %y, %y | test $-1, %y (x)
  3225. j(n)z _Label
  3226. as the first instruction already adjusts the ZF
  3227. %y operand may also be a reference }
  3228. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  3229. MatchOperand(taicpu(p).oper[0]^,-1);
  3230. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  3231. GetLastInstruction(p, hp1) and
  3232. (tai(hp1).typ = ait_instruction) and
  3233. GetNextInstruction(p,hp2) and
  3234. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  3235. case taicpu(hp1).opcode Of
  3236. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  3237. begin
  3238. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3239. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3240. { and in case of carry for A(E)/B(E)/C/NC }
  3241. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  3242. ((taicpu(hp1).opcode <> A_ADD) and
  3243. (taicpu(hp1).opcode <> A_SUB))) then
  3244. begin
  3245. hp1 := tai(p.next);
  3246. asml.remove(p);
  3247. p.free;
  3248. p := tai(hp1);
  3249. Result:=true;
  3250. end;
  3251. end;
  3252. A_SHL, A_SAL, A_SHR, A_SAR:
  3253. begin
  3254. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3255. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  3256. { therefore, it's only safe to do this optimization for }
  3257. { shifts by a (nonzero) constant }
  3258. (taicpu(hp1).oper[0]^.typ = top_const) and
  3259. (taicpu(hp1).oper[0]^.val <> 0) and
  3260. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3261. { and in case of carry for A(E)/B(E)/C/NC }
  3262. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3263. begin
  3264. hp1 := tai(p.next);
  3265. asml.remove(p);
  3266. p.free;
  3267. p := tai(hp1);
  3268. Result:=true;
  3269. end;
  3270. end;
  3271. A_DEC, A_INC, A_NEG:
  3272. begin
  3273. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  3274. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3275. { and in case of carry for A(E)/B(E)/C/NC }
  3276. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3277. begin
  3278. case taicpu(hp1).opcode Of
  3279. A_DEC, A_INC:
  3280. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  3281. begin
  3282. case taicpu(hp1).opcode Of
  3283. A_DEC: taicpu(hp1).opcode := A_SUB;
  3284. A_INC: taicpu(hp1).opcode := A_ADD;
  3285. end;
  3286. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  3287. taicpu(hp1).loadConst(0,1);
  3288. taicpu(hp1).ops:=2;
  3289. end
  3290. end;
  3291. hp1 := tai(p.next);
  3292. asml.remove(p);
  3293. p.free;
  3294. p := tai(hp1);
  3295. Result:=true;
  3296. end;
  3297. end
  3298. else
  3299. { change "test $-1,%reg" into "test %reg,%reg" }
  3300. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3301. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3302. end { case }
  3303. { change "test $-1,%reg" into "test %reg,%reg" }
  3304. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3305. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3306. end;
  3307. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  3308. var
  3309. hp1 : tai;
  3310. hp2 : taicpu;
  3311. begin
  3312. Result:=false;
  3313. {$ifndef x86_64}
  3314. { don't do this on modern CPUs, this really hurts them due to
  3315. broken call/ret pairing }
  3316. if (current_settings.optimizecputype < cpu_Pentium2) and
  3317. not(cs_create_pic in current_settings.moduleswitches) and
  3318. GetNextInstruction(p, hp1) and
  3319. MatchInstruction(hp1,A_JMP,[S_NO]) and
  3320. MatchOpType(taicpu(hp1),top_ref) and
  3321. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  3322. begin
  3323. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  3324. InsertLLItem(p.previous, p, hp2);
  3325. taicpu(p).opcode := A_JMP;
  3326. taicpu(p).is_jmp := true;
  3327. asml.remove(hp1);
  3328. hp1.free;
  3329. Result:=true;
  3330. end
  3331. else
  3332. {$endif x86_64}
  3333. { replace
  3334. call procname
  3335. ret
  3336. by
  3337. jmp procname
  3338. this should never hurt except when pic is used, not sure
  3339. how to handle it then
  3340. but do it only on level 4 because it destroys stack back traces
  3341. }
  3342. if (cs_opt_level4 in current_settings.optimizerswitches) and
  3343. not(cs_create_pic in current_settings.moduleswitches) and
  3344. GetNextInstruction(p, hp1) and
  3345. MatchInstruction(hp1,A_RET,[S_NO]) and
  3346. (taicpu(hp1).ops=0) then
  3347. begin
  3348. taicpu(p).opcode := A_JMP;
  3349. taicpu(p).is_jmp := true;
  3350. asml.remove(hp1);
  3351. hp1.free;
  3352. Result:=true;
  3353. end;
  3354. end;
  3355. {$ifdef x86_64}
  3356. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  3357. var
  3358. PreMessage: string;
  3359. begin
  3360. Result := False;
  3361. { Code size reduction by J. Gareth "Kit" Moreton }
  3362. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  3363. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  3364. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  3365. then
  3366. begin
  3367. { Has 64-bit register name and opcode suffix }
  3368. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  3369. { The actual optimization }
  3370. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3371. if taicpu(p).opsize = S_BQ then
  3372. taicpu(p).changeopsize(S_BL)
  3373. else
  3374. taicpu(p).changeopsize(S_WL);
  3375. DebugMsg(SPeepholeOptimization + PreMessage +
  3376. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  3377. end;
  3378. end;
  3379. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  3380. var
  3381. PreMessage, RegName: string;
  3382. begin
  3383. { Code size reduction by J. Gareth "Kit" Moreton }
  3384. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  3385. as this removes the REX prefix }
  3386. Result := False;
  3387. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  3388. Exit;
  3389. if taicpu(p).oper[0]^.typ <> top_reg then
  3390. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  3391. InternalError(2018011500);
  3392. case taicpu(p).opsize of
  3393. S_Q:
  3394. 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
  3395. begin
  3396. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  3397. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  3398. { The actual optimization }
  3399. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3400. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3401. taicpu(p).changeopsize(S_L);
  3402. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  3403. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  3404. end;
  3405. end;
  3406. end;
  3407. {$endif}
  3408. procedure TX86AsmOptimizer.OptReferences;
  3409. var
  3410. p: tai;
  3411. i: Integer;
  3412. begin
  3413. p := BlockStart;
  3414. while (p <> BlockEnd) Do
  3415. begin
  3416. if p.typ=ait_instruction then
  3417. begin
  3418. for i:=0 to taicpu(p).ops-1 do
  3419. if taicpu(p).oper[i]^.typ=top_ref then
  3420. optimize_ref(taicpu(p).oper[i]^.ref^,false);
  3421. end;
  3422. p:=tai(p.next);
  3423. end;
  3424. end;
  3425. end.