aoptcpu.pas 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the ARM optimizer object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. { $define DEBUG_PREREGSCHEDULER}
  21. {$define DEBUG_AOPTCPU}
  22. Interface
  23. uses
  24. cgbase, cgutils, cpubase, aasmtai,
  25. aasmcpu,
  26. aopt, aoptobj, aoptarm;
  27. Type
  28. TCpuAsmOptimizer = class(TARMAsmOptimizer)
  29. { Can't be done in some cases due to the limited range of jumps }
  30. function CanDoJumpOpts: Boolean; override;
  31. { uses the same constructor as TAopObj }
  32. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  33. procedure PeepHoleOptPass2;override;
  34. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  35. function RemoveSuperfluousVMov(const p : tai; movp : tai; const optimizer : string) : boolean;
  36. { gets the next tai object after current that contains info relevant
  37. to the optimizer in p1 which used the given register or does a
  38. change in program flow.
  39. If there is none, it returns false and
  40. sets p1 to nil }
  41. Function GetNextInstructionUsingRef(Current: tai; Out Next: tai; const ref: TReference; StopOnStore: Boolean = true): Boolean;
  42. { outputs a debug message into the assembler file }
  43. procedure DebugMsg(const s: string; p: tai);
  44. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  45. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  46. protected
  47. function LookForPreindexedPattern(p: taicpu): boolean;
  48. function LookForPostindexedPattern(p: taicpu): boolean;
  49. End;
  50. TCpuPreRegallocScheduler = class(TAsmScheduler)
  51. function SchedulerPass1Cpu(var p: tai): boolean;override;
  52. procedure SwapRegLive(p, hp1: taicpu);
  53. end;
  54. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  55. { uses the same constructor as TAopObj }
  56. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  57. procedure PeepHoleOptPass2;override;
  58. function PostPeepHoleOptsCpu(var p: tai): boolean; override;
  59. End;
  60. function MustBeLast(p : tai) : boolean;
  61. Implementation
  62. uses
  63. cutils,verbose,globtype,globals,
  64. systems,
  65. cpuinfo,
  66. cgobj,procinfo,
  67. aasmbase,aasmdata;
  68. { Range check must be disabled explicitly as conversions between signed and unsigned
  69. 32-bit values are done without explicit typecasts }
  70. {$R-}
  71. function CanBeCond(p : tai) : boolean;
  72. begin
  73. result:=
  74. not(GenerateThumbCode) and
  75. (p.typ=ait_instruction) and
  76. (taicpu(p).condition=C_None) and
  77. ((taicpu(p).opcode<A_IT) or (taicpu(p).opcode>A_ITTTT)) and
  78. (taicpu(p).opcode<>A_CBZ) and
  79. (taicpu(p).opcode<>A_CBNZ) and
  80. (taicpu(p).opcode<>A_PLD) and
  81. (((taicpu(p).opcode<>A_BLX) and
  82. { BL may need to be converted into BLX by the linker -- could possibly
  83. be allowed in case it's to a local symbol of which we know that it
  84. uses the same instruction set as the current one }
  85. (taicpu(p).opcode<>A_BL)) or
  86. (taicpu(p).oper[0]^.typ=top_reg));
  87. end;
  88. function RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList):Boolean;
  89. begin
  90. Result:=false;
  91. if (taicpu(movp).condition = C_EQ) and
  92. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  93. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  94. begin
  95. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  96. asml.remove(movp);
  97. movp.free;
  98. Result:=true;
  99. end;
  100. end;
  101. function AlignedToQWord(const ref : treference) : boolean;
  102. begin
  103. { (safe) heuristics to ensure alignment }
  104. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  105. (((ref.offset>=0) and
  106. ((ref.offset mod 8)=0) and
  107. ((ref.base=NR_R13) or
  108. (ref.index=NR_R13))
  109. ) or
  110. ((ref.offset<=0) and
  111. { when using NR_R11, it has always a value of <qword align>+4 }
  112. ((abs(ref.offset+4) mod 8)=0) and
  113. (current_procinfo.framepointer=NR_R11) and
  114. ((ref.base=NR_R11) or
  115. (ref.index=NR_R11))
  116. )
  117. );
  118. end;
  119. function isValidConstLoadStoreOffset(const aoffset: longint; const pf: TOpPostfix) : boolean;
  120. begin
  121. if GenerateThumb2Code then
  122. result := (aoffset<4096) and (aoffset>-256)
  123. else
  124. result := ((pf in [PF_None,PF_B]) and
  125. (abs(aoffset)<4096)) or
  126. (abs(aoffset)<256);
  127. end;
  128. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  129. var
  130. p: taicpu;
  131. i: longint;
  132. begin
  133. instructionLoadsFromReg := false;
  134. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  135. exit;
  136. p:=taicpu(hp);
  137. i:=1;
  138. {For these instructions we have to start on oper[0]}
  139. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  140. A_CMP, A_CMN, A_TST, A_TEQ,
  141. A_B, A_BL, A_BX, A_BLX,
  142. A_SMLAL, A_UMLAL, A_VSTM, A_VLDM]) then i:=0;
  143. while(i<p.ops) do
  144. begin
  145. case p.oper[I]^.typ of
  146. top_reg:
  147. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  148. { STRD }
  149. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  150. top_regset:
  151. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  152. top_shifterop:
  153. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  154. top_ref:
  155. instructionLoadsFromReg :=
  156. (p.oper[I]^.ref^.base = reg) or
  157. (p.oper[I]^.ref^.index = reg);
  158. else
  159. ;
  160. end;
  161. if instructionLoadsFromReg then exit; {Bailout if we found something}
  162. Inc(I);
  163. end;
  164. end;
  165. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  166. var
  167. p: taicpu;
  168. begin
  169. p := taicpu(hp);
  170. Result := false;
  171. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  172. exit;
  173. case p.opcode of
  174. { These operands do not write into a register at all }
  175. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD,
  176. A_VCMP:
  177. exit;
  178. {Take care of post/preincremented store and loads, they will change their base register}
  179. A_STR, A_LDR:
  180. begin
  181. Result := false;
  182. { actually, this does not apply here because post-/preindexed does not mean that a register
  183. is loaded with a new value, it is only modified
  184. (taicpu(p).oper[1]^.typ=top_ref) and
  185. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  186. (taicpu(p).oper[1]^.ref^.base = reg);
  187. }
  188. { STR does not load into it's first register }
  189. if p.opcode = A_STR then
  190. exit;
  191. end;
  192. A_VSTR:
  193. begin
  194. Result := false;
  195. exit;
  196. end;
  197. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  198. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  199. Result :=
  200. (p.oper[1]^.typ = top_reg) and
  201. (p.oper[1]^.reg = reg);
  202. {Loads to oper2 from coprocessor}
  203. {
  204. MCR/MRC is currently not supported in FPC
  205. A_MRC:
  206. Result :=
  207. (p.oper[2]^.typ = top_reg) and
  208. (p.oper[2]^.reg = reg);
  209. }
  210. {Loads to all register in the registerset}
  211. A_LDM, A_VLDM:
  212. Result := (getsupreg(reg) in p.oper[1]^.regset^);
  213. A_POP:
  214. Result := (getsupreg(reg) in p.oper[0]^.regset^) or
  215. (reg=NR_STACK_POINTER_REG);
  216. else
  217. ;
  218. end;
  219. if Result then
  220. exit;
  221. case p.oper[0]^.typ of
  222. {This is the case}
  223. top_reg:
  224. Result := (p.oper[0]^.reg = reg) or
  225. { LDRD }
  226. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  227. {LDM/STM might write a new value to their index register}
  228. top_ref:
  229. Result :=
  230. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  231. (taicpu(p).oper[0]^.ref^.base = reg);
  232. else
  233. ;
  234. end;
  235. end;
  236. function TCpuAsmOptimizer.GetNextInstructionUsingRef(Current: tai;
  237. Out Next: tai; const ref: TReference; StopOnStore: Boolean = true): Boolean;
  238. begin
  239. Next:=Current;
  240. repeat
  241. Result:=GetNextInstruction(Next,Next);
  242. if Result and
  243. (Next.typ=ait_instruction) and
  244. (taicpu(Next).opcode in [A_LDR, A_STR]) and
  245. (
  246. ((taicpu(Next).ops = 2) and
  247. (taicpu(Next).oper[1]^.typ = top_ref) and
  248. RefsEqual(taicpu(Next).oper[1]^.ref^,ref)) or
  249. ((taicpu(Next).ops = 3) and { LDRD/STRD }
  250. (taicpu(Next).oper[2]^.typ = top_ref) and
  251. RefsEqual(taicpu(Next).oper[2]^.ref^,ref))
  252. ) then
  253. {We've found an instruction LDR or STR with the same reference}
  254. exit;
  255. until not(Result) or
  256. (Next.typ<>ait_instruction) or
  257. not(cs_opt_level3 in current_settings.optimizerswitches) or
  258. is_calljmp(taicpu(Next).opcode) or
  259. (StopOnStore and (taicpu(Next).opcode in [A_STR, A_STM])) or
  260. RegModifiedByInstruction(NR_PC,Next);
  261. Result:=false;
  262. end;
  263. {$ifdef DEBUG_AOPTCPU}
  264. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  265. begin
  266. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  267. end;
  268. {$else DEBUG_AOPTCPU}
  269. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  270. begin
  271. end;
  272. {$endif DEBUG_AOPTCPU}
  273. function TCpuAsmOptimizer.CanDoJumpOpts: Boolean;
  274. begin
  275. { Cannot perform these jump optimisations if the ARM architecture has 16-bit thumb codes }
  276. Result := not (
  277. (current_settings.instructionset = is_thumb) and not (CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype])
  278. );
  279. end;
  280. function TCpuAsmOptimizer.RemoveSuperfluousVMov(const p: tai; movp: tai; const optimizer: string):boolean;
  281. var
  282. alloc,
  283. dealloc : tai_regalloc;
  284. hp1 : tai;
  285. begin
  286. Result:=false;
  287. if ((MatchInstruction(movp, A_VMOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  288. ((getregtype(taicpu(movp).oper[0]^.reg)=R_MMREGISTER) or (taicpu(p).opcode=A_VLDR))
  289. ) or
  290. (((taicpu(p).oppostfix in [PF_F64F32,PF_F64S16,PF_F64S32,PF_F64U16,PF_F64U32]) or (getsubreg(taicpu(p).oper[0]^.reg)=R_SUBFD)) and MatchInstruction(movp, A_VMOV, [taicpu(p).condition], [PF_F64])) or
  291. (((taicpu(p).oppostfix in [PF_F32F64,PF_F32S16,PF_F32S32,PF_F32U16,PF_F32U32]) or (getsubreg(taicpu(p).oper[0]^.reg)=R_SUBFS)) and MatchInstruction(movp, A_VMOV, [taicpu(p).condition], [PF_F32]))
  292. ) and
  293. (taicpu(movp).ops=2) and
  294. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  295. { the destination register of the mov might not be used beween p and movp }
  296. not(RegUsedBetween(taicpu(movp).oper[0]^.reg,p,movp)) and
  297. { Take care to only do this for instructions which REALLY load to the first register.
  298. Otherwise
  299. vstr reg0, [reg1]
  300. vmov reg2, reg0
  301. will be optimized to
  302. vstr reg2, [reg1]
  303. }
  304. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, p) then
  305. begin
  306. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(movp.Next));
  307. if assigned(dealloc) then
  308. begin
  309. DebugMsg('Peephole '+optimizer+' removed superfluous vmov', movp);
  310. result:=true;
  311. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  312. and remove it if possible }
  313. asml.Remove(dealloc);
  314. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.previous));
  315. if assigned(alloc) then
  316. begin
  317. asml.Remove(alloc);
  318. alloc.free;
  319. dealloc.free;
  320. end
  321. else
  322. asml.InsertAfter(dealloc,p);
  323. { try to move the allocation of the target register }
  324. GetLastInstruction(movp,hp1);
  325. alloc:=FindRegAlloc(taicpu(movp).oper[0]^.reg,tai(hp1.Next));
  326. if assigned(alloc) then
  327. begin
  328. asml.Remove(alloc);
  329. asml.InsertBefore(alloc,p);
  330. { adjust used regs }
  331. IncludeRegInUsedRegs(taicpu(movp).oper[0]^.reg,UsedRegs);
  332. end;
  333. { change
  334. vldr reg0,[reg1]
  335. vmov reg2,reg0
  336. into
  337. ldr reg2,[reg1]
  338. if reg2 is an int register
  339. }
  340. if (taicpu(p).opcode=A_VLDR) and (getregtype(taicpu(movp).oper[0]^.reg)=R_INTREGISTER) then
  341. taicpu(p).opcode:=A_LDR;
  342. { finally get rid of the mov }
  343. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  344. asml.remove(movp);
  345. movp.free;
  346. end;
  347. end;
  348. end;
  349. {
  350. optimize
  351. add/sub reg1,reg1,regY/const
  352. ...
  353. ldr/str regX,[reg1]
  354. into
  355. ldr/str regX,[reg1, regY/const]!
  356. }
  357. function TCpuAsmOptimizer.LookForPreindexedPattern(p: taicpu): boolean;
  358. var
  359. hp1: tai;
  360. begin
  361. if GenerateARMCode and
  362. (p.ops=3) and
  363. MatchOperand(p.oper[0]^, p.oper[1]^.reg) and
  364. GetNextInstructionUsingReg(p, hp1, p.oper[0]^.reg) and
  365. (not RegModifiedBetween(p.oper[0]^.reg, p, hp1)) and
  366. MatchInstruction(hp1, [A_LDR,A_STR], [C_None], [PF_None,PF_B,PF_H,PF_SH,PF_SB]) and
  367. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  368. (taicpu(hp1).oper[1]^.ref^.base=p.oper[0]^.reg) and
  369. (taicpu(hp1).oper[0]^.reg<>p.oper[0]^.reg) and
  370. (taicpu(hp1).oper[1]^.ref^.offset=0) and
  371. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  372. (((p.oper[2]^.typ=top_reg) and
  373. (not RegModifiedBetween(p.oper[2]^.reg, p, hp1))) or
  374. ((p.oper[2]^.typ=top_const) and
  375. ((abs(p.oper[2]^.val) < 256) or
  376. ((abs(p.oper[2]^.val) < 4096) and
  377. (taicpu(hp1).oppostfix in [PF_None,PF_B]))))) then
  378. begin
  379. taicpu(hp1).oper[1]^.ref^.addressmode:=AM_PREINDEXED;
  380. if p.oper[2]^.typ=top_reg then
  381. begin
  382. taicpu(hp1).oper[1]^.ref^.index:=p.oper[2]^.reg;
  383. if p.opcode=A_ADD then
  384. taicpu(hp1).oper[1]^.ref^.signindex:=1
  385. else
  386. taicpu(hp1).oper[1]^.ref^.signindex:=-1;
  387. end
  388. else
  389. begin
  390. if p.opcode=A_ADD then
  391. taicpu(hp1).oper[1]^.ref^.offset:=p.oper[2]^.val
  392. else
  393. taicpu(hp1).oper[1]^.ref^.offset:=-p.oper[2]^.val;
  394. end;
  395. result:=true;
  396. end
  397. else
  398. result:=false;
  399. end;
  400. {
  401. optimize
  402. ldr/str regX,[reg1]
  403. ...
  404. add/sub reg1,reg1,regY/const
  405. into
  406. ldr/str regX,[reg1], regY/const
  407. }
  408. function TCpuAsmOptimizer.LookForPostindexedPattern(p: taicpu) : boolean;
  409. var
  410. hp1 : tai;
  411. begin
  412. Result:=false;
  413. if (p.oper[1]^.typ = top_ref) and
  414. (p.oper[1]^.ref^.addressmode=AM_OFFSET) and
  415. (p.oper[1]^.ref^.index=NR_NO) and
  416. (p.oper[1]^.ref^.offset=0) and
  417. GetNextInstructionUsingReg(p, hp1, p.oper[1]^.ref^.base) and
  418. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  419. MatchInstruction(hp1, [A_ADD, A_SUB], [C_None], [PF_None]) and
  420. (taicpu(hp1).oper[0]^.reg=p.oper[1]^.ref^.base) and
  421. (taicpu(hp1).oper[1]^.reg=p.oper[1]^.ref^.base) and
  422. (
  423. (taicpu(hp1).oper[2]^.typ=top_reg) or
  424. { valid offset? }
  425. ((taicpu(hp1).oper[2]^.typ=top_const) and
  426. ((abs(taicpu(hp1).oper[2]^.val)<256) or
  427. ((abs(taicpu(hp1).oper[2]^.val)<4096) and (p.oppostfix in [PF_None,PF_B]))
  428. )
  429. )
  430. ) and
  431. { don't apply the optimization if the base register is loaded }
  432. (p.oper[0]^.reg<>p.oper[1]^.ref^.base) and
  433. not(RegModifiedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) and
  434. { don't apply the optimization if the (new) index register is loaded }
  435. (p.oper[0]^.reg<>taicpu(hp1).oper[2]^.reg) and
  436. not(RegModifiedBetween(taicpu(hp1).oper[2]^.reg,p,hp1)) and
  437. GenerateARMCode then
  438. begin
  439. DebugMsg('Peephole Str/LdrAdd/Sub2Str/Ldr Postindex done', p);
  440. p.oper[1]^.ref^.addressmode:=AM_POSTINDEXED;
  441. if taicpu(hp1).oper[2]^.typ=top_const then
  442. begin
  443. if taicpu(hp1).opcode=A_ADD then
  444. p.oper[1]^.ref^.offset:=taicpu(hp1).oper[2]^.val
  445. else
  446. p.oper[1]^.ref^.offset:=-taicpu(hp1).oper[2]^.val;
  447. end
  448. else
  449. begin
  450. p.oper[1]^.ref^.index:=taicpu(hp1).oper[2]^.reg;
  451. if taicpu(hp1).opcode=A_ADD then
  452. p.oper[1]^.ref^.signindex:=1
  453. else
  454. p.oper[1]^.ref^.signindex:=-1;
  455. end;
  456. asml.Remove(hp1);
  457. hp1.Free;
  458. Result:=true;
  459. end;
  460. end;
  461. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  462. var
  463. hp1,hp2,hp3,hp4: tai;
  464. i, i2: longint;
  465. tempop: tasmop;
  466. oldreg: tregister;
  467. dealloc: tai_regalloc;
  468. function IsPowerOf2(const value: DWord): boolean; inline;
  469. begin
  470. Result:=(value and (value - 1)) = 0;
  471. end;
  472. begin
  473. result := false;
  474. case p.typ of
  475. ait_instruction:
  476. begin
  477. {
  478. change
  479. <op> reg,x,y
  480. cmp reg,#0
  481. into
  482. <op>s reg,x,y
  483. }
  484. { this optimization can applied only to the currently enabled operations because
  485. the other operations do not update all flags and FPC does not track flag usage }
  486. if MatchInstruction(p, [A_ADC,A_ADD,A_BIC,A_SUB,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND,
  487. A_RSB,A_RSC,A_SBC,A_MLA], [C_None], [PF_None]) and
  488. GetNextInstruction(p, hp1) and
  489. { mlas is only allowed in arm mode }
  490. ((taicpu(p).opcode<>A_MLA) or
  491. (current_settings.instructionset<>is_thumb)) and
  492. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  493. (taicpu(hp1).oper[1]^.typ = top_const) and
  494. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  495. (taicpu(hp1).oper[1]^.val = 0) and
  496. GetNextInstruction(hp1, hp2) and
  497. { be careful here, following instructions could use other flags
  498. however after a jump fpc never depends on the value of flags }
  499. { All above instructions set Z and N according to the following
  500. Z := result = 0;
  501. N := result[31];
  502. EQ = Z=1; NE = Z=0;
  503. MI = N=1; PL = N=0; }
  504. (MatchInstruction(hp2, A_B, [C_EQ,C_NE,C_MI,C_PL], []) or
  505. { mov is also possible, but only if there is no shifter operand, it could be an rxx,
  506. we are too lazy to check if it is rxx or something else }
  507. (MatchInstruction(hp2, A_MOV, [C_EQ,C_NE,C_MI,C_PL], []) and (taicpu(hp2).ops=2))) and
  508. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next))) then
  509. begin
  510. DebugMsg('Peephole OpCmp2OpS done', p);
  511. taicpu(p).oppostfix:=PF_S;
  512. { move flag allocation if possible }
  513. GetLastInstruction(hp1, hp2);
  514. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  515. if assigned(hp2) then
  516. begin
  517. asml.Remove(hp2);
  518. asml.insertbefore(hp2, p);
  519. end;
  520. asml.remove(hp1);
  521. hp1.free;
  522. Result:=true;
  523. end
  524. else
  525. case taicpu(p).opcode of
  526. A_STR:
  527. begin
  528. { change
  529. str reg1,ref
  530. ldr reg2,ref
  531. into
  532. str reg1,ref
  533. mov reg2,reg1
  534. }
  535. if (taicpu(p).oper[1]^.typ = top_ref) and
  536. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  537. (taicpu(p).oppostfix=PF_None) and
  538. (taicpu(p).condition=C_None) and
  539. GetNextInstructionUsingRef(p,hp1,taicpu(p).oper[1]^.ref^) and
  540. MatchInstruction(hp1, A_LDR, [taicpu(p).condition], [PF_None]) and
  541. (taicpu(hp1).oper[1]^.typ=top_ref) and
  542. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  543. not(RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  544. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or not (RegModifiedBetween(taicpu(hp1).oper[1]^.ref^.index, p, hp1))) and
  545. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or not (RegModifiedBetween(taicpu(hp1).oper[1]^.ref^.base, p, hp1))) then
  546. begin
  547. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  548. begin
  549. DebugMsg('Peephole StrLdr2StrMov 1 done', hp1);
  550. asml.remove(hp1);
  551. hp1.free;
  552. end
  553. else
  554. begin
  555. taicpu(hp1).opcode:=A_MOV;
  556. taicpu(hp1).oppostfix:=PF_None;
  557. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  558. DebugMsg('Peephole StrLdr2StrMov 2 done', hp1);
  559. end;
  560. result := true;
  561. end
  562. { change
  563. str reg1,ref
  564. str reg2,ref
  565. into
  566. strd reg1,reg2,ref
  567. }
  568. else if (GenerateARMCode or GenerateThumb2Code) and
  569. (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  570. (taicpu(p).oppostfix=PF_None) and
  571. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  572. GetNextInstruction(p,hp1) and
  573. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  574. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  575. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  576. { str ensures that either base or index contain no register, else ldr wouldn't
  577. use an offset either
  578. }
  579. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  580. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  581. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  582. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  583. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  584. begin
  585. DebugMsg('Peephole StrStr2Strd done', p);
  586. taicpu(p).oppostfix:=PF_D;
  587. taicpu(p).loadref(2,taicpu(p).oper[1]^.ref^);
  588. taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
  589. taicpu(p).ops:=3;
  590. asml.remove(hp1);
  591. hp1.free;
  592. result:=true;
  593. end;
  594. Result:=LookForPostindexedPattern(taicpu(p)) or Result;
  595. end;
  596. A_LDR:
  597. begin
  598. { change
  599. ldr reg1,ref
  600. ldr reg2,ref
  601. into ...
  602. }
  603. if (taicpu(p).oper[1]^.typ = top_ref) and
  604. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  605. GetNextInstruction(p,hp1) and
  606. { ldrd is not allowed here }
  607. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  608. begin
  609. {
  610. ...
  611. ldr reg1,ref
  612. mov reg2,reg1
  613. }
  614. if (taicpu(p).oppostfix=taicpu(hp1).oppostfix) and
  615. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  616. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  617. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  618. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  619. begin
  620. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  621. begin
  622. DebugMsg('Peephole LdrLdr2Ldr done', hp1);
  623. asml.remove(hp1);
  624. hp1.free;
  625. end
  626. else
  627. begin
  628. DebugMsg('Peephole LdrLdr2LdrMov done', hp1);
  629. taicpu(hp1).opcode:=A_MOV;
  630. taicpu(hp1).oppostfix:=PF_None;
  631. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  632. end;
  633. result := true;
  634. end
  635. {
  636. ...
  637. ldrd reg1,reg1+1,ref
  638. }
  639. else if (GenerateARMCode or GenerateThumb2Code) and
  640. (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  641. { ldrd does not allow any postfixes ... }
  642. (taicpu(p).oppostfix=PF_None) and
  643. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  644. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  645. { ldr ensures that either base or index contain no register, else ldr wouldn't
  646. use an offset either
  647. }
  648. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  649. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  650. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  651. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  652. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  653. begin
  654. DebugMsg('Peephole LdrLdr2Ldrd done', p);
  655. taicpu(p).loadref(2,taicpu(p).oper[1]^.ref^);
  656. taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
  657. taicpu(p).ops:=3;
  658. taicpu(p).oppostfix:=PF_D;
  659. asml.remove(hp1);
  660. hp1.free;
  661. result:=true;
  662. end;
  663. end;
  664. {
  665. Change
  666. ldrb dst1, [REF]
  667. and dst2, dst1, #255
  668. into
  669. ldrb dst2, [ref]
  670. }
  671. if not(GenerateThumbCode) and
  672. (taicpu(p).oppostfix=PF_B) and
  673. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  674. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_NONE]) and
  675. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  676. (taicpu(hp1).oper[2]^.typ = top_const) and
  677. (taicpu(hp1).oper[2]^.val = $FF) and
  678. not(RegUsedBetween(taicpu(hp1).oper[0]^.reg, p, hp1)) and
  679. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  680. begin
  681. DebugMsg('Peephole LdrbAnd2Ldrb done', p);
  682. taicpu(p).oper[0]^.reg := taicpu(hp1).oper[0]^.reg;
  683. asml.remove(hp1);
  684. hp1.free;
  685. result:=true;
  686. end;
  687. Result:=LookForPostindexedPattern(taicpu(p)) or Result;
  688. { Remove superfluous mov after ldr
  689. changes
  690. ldr reg1, ref
  691. mov reg2, reg1
  692. to
  693. ldr reg2, ref
  694. conditions are:
  695. * no ldrd usage
  696. * reg1 must be released after mov
  697. * mov can not contain shifterops
  698. * ldr+mov have the same conditions
  699. * mov does not set flags
  700. }
  701. if (taicpu(p).oppostfix<>PF_D) and
  702. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  703. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr') then
  704. Result:=true;
  705. end;
  706. A_MOV:
  707. begin
  708. { fold
  709. mov reg1,reg0, shift imm1
  710. mov reg1,reg1, shift imm2
  711. }
  712. if (taicpu(p).ops=3) and
  713. (taicpu(p).oper[2]^.typ = top_shifterop) and
  714. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  715. getnextinstruction(p,hp1) and
  716. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  717. (taicpu(hp1).ops=3) and
  718. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  719. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  720. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  721. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  722. begin
  723. { fold
  724. mov reg1,reg0, lsl 16
  725. mov reg1,reg1, lsr 16
  726. strh reg1, ...
  727. dealloc reg1
  728. to
  729. strh reg1, ...
  730. dealloc reg1
  731. }
  732. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  733. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  734. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  735. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  736. getnextinstruction(hp1,hp2) and
  737. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  738. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  739. begin
  740. TransferUsedRegs(TmpUsedRegs);
  741. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  742. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  743. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  744. begin
  745. DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
  746. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  747. asml.remove(p);
  748. asml.remove(hp1);
  749. p.free;
  750. hp1.free;
  751. p:=hp2;
  752. Result:=true;
  753. end;
  754. end
  755. { fold
  756. mov reg1,reg0, shift imm1
  757. mov reg1,reg1, shift imm2
  758. to
  759. mov reg1,reg0, shift imm1+imm2
  760. }
  761. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  762. { asr makes no use after a lsr, the asr can be foled into the lsr }
  763. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  764. begin
  765. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  766. { avoid overflows }
  767. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  768. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  769. SM_ROR:
  770. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  771. SM_ASR:
  772. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  773. SM_LSR,
  774. SM_LSL:
  775. begin
  776. hp2:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  777. InsertLLItem(p.previous, p.next, hp2);
  778. p.free;
  779. p:=hp2;
  780. end;
  781. else
  782. internalerror(2008072803);
  783. end;
  784. DebugMsg('Peephole ShiftShift2Shift 1 done', p);
  785. asml.remove(hp1);
  786. hp1.free;
  787. result := true;
  788. end
  789. { fold
  790. mov reg1,reg0, shift imm1
  791. mov reg1,reg1, shift imm2
  792. mov reg1,reg1, shift imm3 ...
  793. mov reg2,reg1, shift imm3 ...
  794. }
  795. else if GetNextInstructionUsingReg(hp1,hp2, taicpu(hp1).oper[0]^.reg) and
  796. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  797. (taicpu(hp2).ops=3) and
  798. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  799. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp2)) and
  800. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  801. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  802. begin
  803. { mov reg1,reg0, lsl imm1
  804. mov reg1,reg1, lsr/asr imm2
  805. mov reg2,reg1, lsl imm3 ...
  806. to
  807. mov reg1,reg0, lsl imm1
  808. mov reg2,reg1, lsr/asr imm2-imm3
  809. if
  810. imm1>=imm2
  811. }
  812. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  813. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  814. (taicpu(p).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  815. begin
  816. if (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  817. begin
  818. if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,p,hp1)) and
  819. not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  820. begin
  821. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1a done', p);
  822. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm-taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  823. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  824. asml.remove(hp1);
  825. asml.remove(hp2);
  826. hp1.free;
  827. hp2.free;
  828. if taicpu(p).oper[2]^.shifterop^.shiftimm>=32 then
  829. begin
  830. taicpu(p).freeop(1);
  831. taicpu(p).freeop(2);
  832. taicpu(p).loadconst(1,0);
  833. end;
  834. result := true;
  835. end;
  836. end
  837. else if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  838. begin
  839. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1b done', p);
  840. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  841. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  842. asml.remove(hp2);
  843. hp2.free;
  844. result := true;
  845. end;
  846. end
  847. { mov reg1,reg0, lsr/asr imm1
  848. mov reg1,reg1, lsl imm2
  849. mov reg1,reg1, lsr/asr imm3 ...
  850. if imm3>=imm1 and imm2>=imm1
  851. to
  852. mov reg1,reg0, lsl imm2-imm1
  853. mov reg1,reg1, lsr/asr imm3 ...
  854. }
  855. else if (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  856. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  857. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  858. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  859. begin
  860. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  861. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  862. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  863. asml.remove(p);
  864. p.free;
  865. p:=hp2;
  866. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  867. begin
  868. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  869. asml.remove(hp1);
  870. hp1.free;
  871. p:=hp2;
  872. end;
  873. result := true;
  874. end;
  875. end;
  876. end;
  877. { Change the common
  878. mov r0, r0, lsr #xxx
  879. and r0, r0, #yyy/bic r0, r0, #xxx
  880. and remove the superfluous and/bic if possible
  881. This could be extended to handle more cases.
  882. }
  883. if (taicpu(p).ops=3) and
  884. (taicpu(p).oper[2]^.typ = top_shifterop) and
  885. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  886. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  887. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  888. (hp1.typ=ait_instruction) and
  889. (taicpu(hp1).ops>=1) and
  890. (taicpu(hp1).oper[0]^.typ=top_reg) and
  891. (not RegModifiedBetween(taicpu(hp1).oper[0]^.reg, p, hp1)) and
  892. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  893. begin
  894. if (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  895. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  896. (taicpu(hp1).ops=3) and
  897. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  898. (taicpu(hp1).oper[2]^.typ = top_const) and
  899. { Check if the AND actually would only mask out bits being already zero because of the shift
  900. }
  901. ((($ffffffff shr taicpu(p).oper[2]^.shifterop^.shiftimm) and taicpu(hp1).oper[2]^.val) =
  902. ($ffffffff shr taicpu(p).oper[2]^.shifterop^.shiftimm)) then
  903. begin
  904. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  905. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  906. asml.remove(hp1);
  907. hp1.free;
  908. result:=true;
  909. end
  910. else if MatchInstruction(hp1, A_BIC, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  911. (taicpu(hp1).ops=3) and
  912. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  913. (taicpu(hp1).oper[2]^.typ = top_const) and
  914. { Check if the BIC actually would only mask out bits beeing already zero because of the shift }
  915. (taicpu(hp1).oper[2]^.val<>0) and
  916. (BsfDWord(taicpu(hp1).oper[2]^.val)>=32-taicpu(p).oper[2]^.shifterop^.shiftimm) then
  917. begin
  918. DebugMsg('Peephole LsrBic2Lsr done', hp1);
  919. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  920. asml.remove(hp1);
  921. hp1.free;
  922. result:=true;
  923. end;
  924. end;
  925. { Change
  926. mov rx, ry, lsr/ror #xxx
  927. uxtb/uxth rz,rx/and rz,rx,0xFF
  928. dealloc rx
  929. to
  930. uxtb/uxth rz,ry,ror #xxx
  931. }
  932. if (taicpu(p).ops=3) and
  933. (taicpu(p).oper[2]^.typ = top_shifterop) and
  934. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  935. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ROR]) and
  936. (GenerateThumb2Code) and
  937. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  938. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  939. begin
  940. if MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  941. (taicpu(hp1).ops = 2) and
  942. (taicpu(p).oper[2]^.shifterop^.shiftimm in [8,16,24]) and
  943. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  944. begin
  945. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  946. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  947. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  948. taicpu(hp1).ops := 3;
  949. GetNextInstruction(p,hp1);
  950. asml.Remove(p);
  951. p.Free;
  952. p:=hp1;
  953. result:=true;
  954. exit;
  955. end
  956. else if MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  957. (taicpu(hp1).ops=2) and
  958. (taicpu(p).oper[2]^.shifterop^.shiftimm in [16]) and
  959. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  960. begin
  961. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  962. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  963. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  964. taicpu(hp1).ops := 3;
  965. GetNextInstruction(p,hp1);
  966. asml.Remove(p);
  967. p.Free;
  968. p:=hp1;
  969. result:=true;
  970. exit;
  971. end
  972. else if MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  973. (taicpu(hp1).ops = 3) and
  974. (taicpu(hp1).oper[2]^.typ = top_const) and
  975. (taicpu(hp1).oper[2]^.val = $FF) and
  976. (taicpu(p).oper[2]^.shifterop^.shiftimm in [8,16,24]) and
  977. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  978. begin
  979. taicpu(hp1).ops := 3;
  980. taicpu(hp1).opcode := A_UXTB;
  981. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  982. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  983. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  984. GetNextInstruction(p,hp1);
  985. asml.Remove(p);
  986. p.Free;
  987. p:=hp1;
  988. result:=true;
  989. exit;
  990. end;
  991. end;
  992. {
  993. optimize
  994. mov rX, yyyy
  995. ....
  996. }
  997. if (taicpu(p).ops = 2) and
  998. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  999. (tai(hp1).typ = ait_instruction) then
  1000. begin
  1001. {
  1002. This removes the mul from
  1003. mov rX,0
  1004. ...
  1005. mul ...,rX,...
  1006. }
  1007. if false and (taicpu(p).oper[1]^.typ = top_const) and
  1008. (taicpu(p).oper[1]^.val=0) and
  1009. MatchInstruction(hp1, [A_MUL,A_MLA], [taicpu(p).condition], [taicpu(p).oppostfix]) and
  1010. (((taicpu(hp1).oper[1]^.typ=top_reg) and MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^)) or
  1011. ((taicpu(hp1).oper[2]^.typ=top_reg) and MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[2]^))) then
  1012. begin
  1013. TransferUsedRegs(TmpUsedRegs);
  1014. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1015. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1016. DebugMsg('Peephole MovMUL/MLA2Mov0 done', p);
  1017. if taicpu(hp1).opcode=A_MUL then
  1018. taicpu(hp1).loadconst(1,0)
  1019. else
  1020. taicpu(hp1).loadreg(1,taicpu(hp1).oper[3]^.reg);
  1021. taicpu(hp1).ops:=2;
  1022. taicpu(hp1).opcode:=A_MOV;
  1023. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  1024. RemoveCurrentP(p);
  1025. Result:=true;
  1026. exit;
  1027. end
  1028. else if (taicpu(p).oper[1]^.typ = top_const) and
  1029. (taicpu(p).oper[1]^.val=0) and
  1030. MatchInstruction(hp1, A_MLA, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  1031. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[3]^) then
  1032. begin
  1033. TransferUsedRegs(TmpUsedRegs);
  1034. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1035. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1036. DebugMsg('Peephole MovMLA2MUL 1 done', p);
  1037. taicpu(hp1).ops:=3;
  1038. taicpu(hp1).opcode:=A_MUL;
  1039. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  1040. RemoveCurrentP(p);
  1041. Result:=true;
  1042. exit;
  1043. end
  1044. {
  1045. This changes the very common
  1046. mov r0, #0
  1047. str r0, [...]
  1048. mov r0, #0
  1049. str r0, [...]
  1050. and removes all superfluous mov instructions
  1051. }
  1052. else if (taicpu(p).oper[1]^.typ = top_const) and
  1053. (taicpu(hp1).opcode=A_STR) then
  1054. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  1055. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  1056. GetNextInstruction(hp1, hp2) and
  1057. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1058. (taicpu(hp2).ops = 2) and
  1059. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  1060. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  1061. begin
  1062. DebugMsg('Peephole MovStrMov done', hp2);
  1063. GetNextInstruction(hp2,hp1);
  1064. asml.remove(hp2);
  1065. hp2.free;
  1066. result:=true;
  1067. if not assigned(hp1) then break;
  1068. end
  1069. {
  1070. This removes the first mov from
  1071. mov rX,...
  1072. mov rX,...
  1073. }
  1074. else if taicpu(hp1).opcode=A_MOV then
  1075. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  1076. (taicpu(hp1).ops = 2) and
  1077. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  1078. { don't remove the first mov if the second is a mov rX,rX }
  1079. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  1080. begin
  1081. DebugMsg('Peephole MovMov done', p);
  1082. asml.remove(p);
  1083. p.free;
  1084. p:=hp1;
  1085. GetNextInstruction(hp1,hp1);
  1086. result:=true;
  1087. if not assigned(hp1) then
  1088. break;
  1089. end;
  1090. end;
  1091. {
  1092. change
  1093. mov r1, r0
  1094. add r1, r1, #1
  1095. to
  1096. add r1, r0, #1
  1097. Todo: Make it work for mov+cmp too
  1098. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1099. }
  1100. if (taicpu(p).ops = 2) and
  1101. (taicpu(p).oper[1]^.typ = top_reg) and
  1102. (taicpu(p).oppostfix = PF_NONE) and
  1103. GetNextInstruction(p, hp1) and
  1104. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1105. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  1106. [taicpu(p).condition], []) and
  1107. {MOV and MVN might only have 2 ops}
  1108. (taicpu(hp1).ops >= 2) and
  1109. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  1110. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1111. (
  1112. (taicpu(hp1).ops = 2) or
  1113. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop])
  1114. ) then
  1115. begin
  1116. { When we get here we still don't know if the registers match}
  1117. for I:=1 to 2 do
  1118. {
  1119. If the first loop was successful p will be replaced with hp1.
  1120. The checks will still be ok, because all required information
  1121. will also be in hp1 then.
  1122. }
  1123. if (taicpu(hp1).ops > I) and
  1124. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) and
  1125. { prevent certain combinations on thumb(2), this is only a safe approximation }
  1126. (not(GenerateThumbCode or GenerateThumb2Code) or
  1127. ((getsupreg(taicpu(p).oper[1]^.reg)<>RS_R13) and
  1128. (getsupreg(taicpu(p).oper[1]^.reg)<>RS_R15))
  1129. ) then
  1130. begin
  1131. DebugMsg('Peephole RedundantMovProcess done', hp1);
  1132. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  1133. if p<>hp1 then
  1134. begin
  1135. asml.remove(p);
  1136. p.free;
  1137. p:=hp1;
  1138. Result:=true;
  1139. end;
  1140. end;
  1141. end;
  1142. { Fold the very common sequence
  1143. mov regA, regB
  1144. ldr* regA, [regA]
  1145. to
  1146. ldr* regA, [regB]
  1147. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1148. }
  1149. if (taicpu(p).opcode = A_MOV) and
  1150. (taicpu(p).ops = 2) and
  1151. (taicpu(p).oper[1]^.typ = top_reg) and
  1152. (taicpu(p).oppostfix = PF_NONE) and
  1153. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1154. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], []) and
  1155. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1156. { We can change the base register only when the instruction uses AM_OFFSET }
  1157. ((taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) or
  1158. ((taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) and
  1159. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg))
  1160. ) and
  1161. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1162. // Make sure that Thumb code doesn't propagate a high register into a reference
  1163. ((GenerateThumbCode and
  1164. (getsupreg(taicpu(p).oper[1]^.reg) < RS_R8)) or
  1165. (not GenerateThumbCode)) and
  1166. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1167. begin
  1168. DebugMsg('Peephole MovLdr2Ldr done', hp1);
  1169. if (taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) and
  1170. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1171. taicpu(hp1).oper[1]^.ref^.base := taicpu(p).oper[1]^.reg;
  1172. if taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg then
  1173. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1174. dealloc:=FindRegDeAlloc(taicpu(p).oper[1]^.reg, tai(p.Next));
  1175. if Assigned(dealloc) then
  1176. begin
  1177. asml.remove(dealloc);
  1178. asml.InsertAfter(dealloc,hp1);
  1179. end;
  1180. GetNextInstruction(p, hp1);
  1181. asml.remove(p);
  1182. p.free;
  1183. p:=hp1;
  1184. result:=true;
  1185. end;
  1186. { This folds shifterops into following instructions
  1187. mov r0, r1, lsl #8
  1188. add r2, r3, r0
  1189. to
  1190. add r2, r3, r1, lsl #8
  1191. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1192. }
  1193. if (taicpu(p).opcode = A_MOV) and
  1194. (taicpu(p).ops = 3) and
  1195. (taicpu(p).oper[1]^.typ = top_reg) and
  1196. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1197. (taicpu(p).oppostfix = PF_NONE) and
  1198. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1199. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1200. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  1201. A_CMP, A_CMN],
  1202. [taicpu(p).condition], [PF_None]) and
  1203. (not ((GenerateThumb2Code) and
  1204. (taicpu(hp1).opcode in [A_SBC]) and
  1205. (((taicpu(hp1).ops=3) and
  1206. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^.reg)) or
  1207. ((taicpu(hp1).ops=2) and
  1208. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg))))) and
  1209. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) and
  1210. (taicpu(hp1).ops >= 2) and
  1211. {Currently we can't fold into another shifterop}
  1212. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  1213. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  1214. NR_DEFAULTFLAGS for modification}
  1215. (
  1216. {Everything is fine if we don't use RRX}
  1217. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  1218. (
  1219. {If it is RRX, then check if we're just accessing the next instruction}
  1220. GetNextInstruction(p, hp2) and
  1221. (hp1 = hp2)
  1222. )
  1223. ) and
  1224. { reg1 might not be modified inbetween }
  1225. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1226. { The shifterop can contain a register, might not be modified}
  1227. (
  1228. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  1229. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  1230. ) and
  1231. (
  1232. {Only ONE of the two src operands is allowed to match}
  1233. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  1234. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  1235. ) then
  1236. begin
  1237. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  1238. I2:=0
  1239. else
  1240. I2:=1;
  1241. for I:=I2 to taicpu(hp1).ops-1 do
  1242. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  1243. begin
  1244. { If the parameter matched on the second op from the RIGHT
  1245. we have to switch the parameters, this will not happen for CMP
  1246. were we're only evaluating the most right parameter
  1247. }
  1248. if I <> taicpu(hp1).ops-1 then
  1249. begin
  1250. {The SUB operators need to be changed when we swap parameters}
  1251. case taicpu(hp1).opcode of
  1252. A_SUB: tempop:=A_RSB;
  1253. A_SBC: tempop:=A_RSC;
  1254. A_RSB: tempop:=A_SUB;
  1255. A_RSC: tempop:=A_SBC;
  1256. else tempop:=taicpu(hp1).opcode;
  1257. end;
  1258. if taicpu(hp1).ops = 3 then
  1259. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1260. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1261. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1262. else
  1263. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1264. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1265. taicpu(p).oper[2]^.shifterop^);
  1266. end
  1267. else
  1268. if taicpu(hp1).ops = 3 then
  1269. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1270. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1271. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1272. else
  1273. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1274. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1275. taicpu(p).oper[2]^.shifterop^);
  1276. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  1277. AllocRegBetween(taicpu(p).oper[2]^.shifterop^.rs,p,hp1,UsedRegs);
  1278. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
  1279. asml.insertbefore(hp2, hp1);
  1280. GetNextInstruction(p, hp2);
  1281. asml.remove(p);
  1282. asml.remove(hp1);
  1283. p.free;
  1284. hp1.free;
  1285. p:=hp2;
  1286. DebugMsg('Peephole FoldShiftProcess done', p);
  1287. Result:=true;
  1288. break;
  1289. end;
  1290. end;
  1291. {
  1292. Fold
  1293. mov r1, r1, lsl #2
  1294. ldr/ldrb r0, [r0, r1]
  1295. to
  1296. ldr/ldrb r0, [r0, r1, lsl #2]
  1297. XXX: This still needs some work, as we quite often encounter something like
  1298. mov r1, r2, lsl #2
  1299. add r2, r3, #imm
  1300. ldr r0, [r2, r1]
  1301. which can't be folded because r2 is overwritten between the shift and the ldr.
  1302. We could try to shuffle the registers around and fold it into.
  1303. add r1, r3, #imm
  1304. ldr r0, [r1, r2, lsl #2]
  1305. }
  1306. if (not(GenerateThumbCode)) and
  1307. (taicpu(p).opcode = A_MOV) and
  1308. (taicpu(p).ops = 3) and
  1309. (taicpu(p).oper[1]^.typ = top_reg) and
  1310. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1311. { RRX is tough to handle, because it requires tracking the C-Flag,
  1312. it is also extremly unlikely to be emitted this way}
  1313. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1314. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1315. { thumb2 allows only lsl #0..#3 }
  1316. (not(GenerateThumb2Code) or
  1317. ((taicpu(p).oper[2]^.shifterop^.shiftimm in [0..3]) and
  1318. (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL)
  1319. )
  1320. ) and
  1321. (taicpu(p).oppostfix = PF_NONE) and
  1322. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1323. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1324. (MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B]) or
  1325. (GenerateThumb2Code and
  1326. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B, PF_SB, PF_H, PF_SH]))
  1327. ) and
  1328. (
  1329. {If this is address by offset, one of the two registers can be used}
  1330. ((taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1331. (
  1332. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) xor
  1333. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg)
  1334. )
  1335. ) or
  1336. {For post and preindexed only the index register can be used}
  1337. ((taicpu(hp1).oper[1]^.ref^.addressmode in [AM_POSTINDEXED, AM_PREINDEXED]) and
  1338. (
  1339. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1340. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg)
  1341. ) and
  1342. (not GenerateThumb2Code)
  1343. )
  1344. ) and
  1345. { Only fold if both registers are used. Otherwise we are folding p with itself }
  1346. (taicpu(hp1).oper[1]^.ref^.index<>NR_NO) and
  1347. (taicpu(hp1).oper[1]^.ref^.base<>NR_NO) and
  1348. { Only fold if there isn't another shifterop already, and offset is zero. }
  1349. (taicpu(hp1).oper[1]^.ref^.offset = 0) and
  1350. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1351. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1352. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1353. begin
  1354. { If the register we want to do the shift for resides in base, we need to swap that}
  1355. if (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1356. taicpu(hp1).oper[1]^.ref^.base := taicpu(hp1).oper[1]^.ref^.index;
  1357. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1358. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1359. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1360. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1361. GetNextInstruction(p, hp1);
  1362. asml.remove(p);
  1363. p.free;
  1364. p:=hp1;
  1365. Result:=true;
  1366. end;
  1367. {
  1368. Often we see shifts and then a superfluous mov to another register
  1369. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1370. }
  1371. if (taicpu(p).opcode = A_MOV) and
  1372. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1373. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov') then
  1374. Result:=true;
  1375. end;
  1376. A_ADD,
  1377. A_ADC,
  1378. A_RSB,
  1379. A_RSC,
  1380. A_SUB,
  1381. A_SBC,
  1382. A_AND,
  1383. A_BIC,
  1384. A_EOR,
  1385. A_ORR,
  1386. A_MLA,
  1387. A_MLS,
  1388. A_MUL,
  1389. A_QADD,A_QADD16,A_QADD8,
  1390. A_QSUB,A_QSUB16,A_QSUB8,
  1391. A_QDADD,A_QDSUB,A_QASX,A_QSAX,
  1392. A_SHADD16,A_SHADD8,A_UHADD16,A_UHADD8,
  1393. A_SHSUB16,A_SHSUB8,A_UHSUB16,A_UHSUB8,
  1394. A_PKHTB,A_PKHBT,
  1395. A_SMUAD,A_SMUSD:
  1396. begin
  1397. {
  1398. optimize
  1399. and reg2,reg1,const1
  1400. ...
  1401. }
  1402. if (taicpu(p).opcode = A_AND) and
  1403. (taicpu(p).ops>2) and
  1404. (taicpu(p).oper[1]^.typ = top_reg) and
  1405. (taicpu(p).oper[2]^.typ = top_const) then
  1406. begin
  1407. {
  1408. change
  1409. and reg2,reg1,const1
  1410. ...
  1411. and reg3,reg2,const2
  1412. to
  1413. and reg3,reg1,(const1 and const2)
  1414. }
  1415. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1416. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1417. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1418. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1419. (taicpu(hp1).oper[2]^.typ = top_const) then
  1420. begin
  1421. if not(RegUsedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) then
  1422. begin
  1423. DebugMsg('Peephole AndAnd2And done', p);
  1424. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  1425. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1426. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1427. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1428. asml.remove(hp1);
  1429. hp1.free;
  1430. Result:=true;
  1431. end
  1432. else if not(RegUsedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1433. begin
  1434. DebugMsg('Peephole AndAnd2And done', hp1);
  1435. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
  1436. taicpu(hp1).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1437. taicpu(hp1).oppostfix:=taicpu(p).oppostfix;
  1438. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1439. GetNextInstruction(p, hp1);
  1440. RemoveCurrentP(p);
  1441. p:=hp1;
  1442. Result:=true;
  1443. end;
  1444. end
  1445. {
  1446. change
  1447. and reg2,reg1,$xxxxxxFF
  1448. strb reg2,[...]
  1449. dealloc reg2
  1450. to
  1451. strb reg1,[...]
  1452. }
  1453. else if ((taicpu(p).oper[2]^.val and $FF) = $FF) and
  1454. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1455. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1456. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1457. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1458. { the reference in strb might not use reg2 }
  1459. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1460. { reg1 might not be modified inbetween }
  1461. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1462. begin
  1463. DebugMsg('Peephole AndStrb2Strb done', p);
  1464. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1465. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
  1466. GetNextInstruction(p, hp1);
  1467. RemoveCurrentP(p);
  1468. p:=hp1;
  1469. result:=true;
  1470. end
  1471. {
  1472. change
  1473. and reg2,reg1,255
  1474. uxtb/uxth reg3,reg2
  1475. dealloc reg2
  1476. to
  1477. and reg3,reg1,x
  1478. }
  1479. else if (taicpu(p).oper[2]^.val = $FF) and
  1480. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1481. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1482. MatchInstruction(hp1, [A_UXTB,A_UXTH], [C_None], [PF_None]) and
  1483. (taicpu(hp1).ops = 2) and
  1484. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1485. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1486. { reg1 might not be modified inbetween }
  1487. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1488. begin
  1489. DebugMsg('Peephole AndUxt2And done', p);
  1490. taicpu(hp1).opcode:=A_AND;
  1491. taicpu(hp1).ops:=3;
  1492. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1493. taicpu(hp1).loadconst(2,255);
  1494. GetNextInstruction(p,hp1);
  1495. asml.remove(p);
  1496. p.Free;
  1497. p:=hp1;
  1498. result:=true;
  1499. end
  1500. {
  1501. from
  1502. and reg1,reg0,2^n-1
  1503. mov reg2,reg1, lsl imm1
  1504. (mov reg3,reg2, lsr/asr imm1)
  1505. remove either the and or the lsl/xsr sequence if possible
  1506. }
  1507. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1508. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1509. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1510. (taicpu(hp1).ops=3) and
  1511. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1512. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1513. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1514. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1515. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) then
  1516. begin
  1517. {
  1518. and reg1,reg0,2^n-1
  1519. mov reg2,reg1, lsl imm1
  1520. mov reg3,reg2, lsr/asr imm1
  1521. =>
  1522. and reg1,reg0,2^n-1
  1523. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1524. }
  1525. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1526. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1527. (taicpu(hp2).ops=3) and
  1528. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1529. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1530. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1531. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1532. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1533. RegEndOfLife(taicpu(hp1).oper[0]^.reg,taicpu(hp2)) and
  1534. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1535. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1536. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1537. begin
  1538. DebugMsg('Peephole AndLslXsr2And done', p);
  1539. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1540. asml.Remove(hp1);
  1541. asml.Remove(hp2);
  1542. hp1.free;
  1543. hp2.free;
  1544. result:=true;
  1545. end
  1546. {
  1547. and reg1,reg0,2^n-1
  1548. mov reg2,reg1, lsl imm1
  1549. =>
  1550. mov reg2,reg0, lsl imm1
  1551. if imm1>i
  1552. }
  1553. else if (i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1554. not(RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) then
  1555. begin
  1556. DebugMsg('Peephole AndLsl2Lsl done', p);
  1557. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  1558. GetNextInstruction(p, hp1);
  1559. asml.Remove(p);
  1560. p.free;
  1561. p:=hp1;
  1562. result:=true;
  1563. end
  1564. end;
  1565. end;
  1566. {
  1567. change
  1568. add/sub reg2,reg1,const1
  1569. str/ldr reg3,[reg2,const2]
  1570. dealloc reg2
  1571. to
  1572. str/ldr reg3,[reg1,const2+/-const1]
  1573. }
  1574. if (not GenerateThumbCode) and
  1575. (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1576. (taicpu(p).ops>2) and
  1577. (taicpu(p).oper[1]^.typ = top_reg) and
  1578. (taicpu(p).oper[2]^.typ = top_const) then
  1579. begin
  1580. hp1:=p;
  1581. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1582. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1583. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1584. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1585. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1586. { don't optimize if the register is stored/overwritten }
  1587. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1588. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1589. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1590. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1591. ldr postfix }
  1592. (((taicpu(p).opcode=A_ADD) and
  1593. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1594. ) or
  1595. ((taicpu(p).opcode=A_SUB) and
  1596. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1597. )
  1598. ) do
  1599. begin
  1600. { neither reg1 nor reg2 might be changed inbetween }
  1601. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1602. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1603. break;
  1604. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1605. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1606. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1607. begin
  1608. { remember last instruction }
  1609. hp2:=hp1;
  1610. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1611. hp1:=p;
  1612. { fix all ldr/str }
  1613. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1614. begin
  1615. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1616. if taicpu(p).opcode=A_ADD then
  1617. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1618. else
  1619. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1620. if hp1=hp2 then
  1621. break;
  1622. end;
  1623. GetNextInstruction(p,hp1);
  1624. asml.remove(p);
  1625. p.free;
  1626. p:=hp1;
  1627. result:=true;
  1628. break;
  1629. end;
  1630. end;
  1631. end;
  1632. {
  1633. change
  1634. add reg1, ...
  1635. mov reg2, reg1
  1636. to
  1637. add reg2, ...
  1638. }
  1639. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1640. (taicpu(p).ops>=3) and
  1641. RemoveSuperfluousMove(p, hp1, 'DataMov2Data') then
  1642. Result:=true;
  1643. if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1644. LookForPreindexedPattern(taicpu(p)) then
  1645. begin
  1646. GetNextInstruction(p,hp1);
  1647. DebugMsg('Peephole Add/Sub to Preindexed done', p);
  1648. asml.remove(p);
  1649. p.free;
  1650. p:=hp1;
  1651. Result:=true;
  1652. end;
  1653. {
  1654. Turn
  1655. mul reg0, z,w
  1656. sub/add x, y, reg0
  1657. dealloc reg0
  1658. into
  1659. mls/mla x,z,w,y
  1660. }
  1661. if MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  1662. (taicpu(p).ops=3) and
  1663. (taicpu(p).oper[0]^.typ = top_reg) and
  1664. (taicpu(p).oper[1]^.typ = top_reg) and
  1665. (taicpu(p).oper[2]^.typ = top_reg) and
  1666. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1667. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  1668. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  1669. (not RegModifiedBetween(taicpu(p).oper[2]^.reg, p, hp1)) and
  1670. (((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype>=cpu_armv4)) or
  1671. ((taicpu(hp1).opcode=A_SUB) and (current_settings.cputype in [cpu_armv6t2,cpu_armv7,cpu_armv7a,cpu_armv7r,cpu_armv7m,cpu_armv7em]))) and
  1672. // CPUs before ARMv6 don't recommend having the same Rd and Rm for MLA.
  1673. // TODO: A workaround would be to swap Rm and Rs
  1674. (not ((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype<=cpu_armv6) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^))) and
  1675. (((taicpu(hp1).ops=3) and
  1676. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1677. ((MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) and
  1678. (not RegModifiedBetween(taicpu(hp1).oper[1]^.reg, p, hp1))) or
  1679. ((MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1680. (taicpu(hp1).opcode=A_ADD) and
  1681. (not RegModifiedBetween(taicpu(hp1).oper[2]^.reg, p, hp1)))))) or
  1682. ((taicpu(hp1).ops=2) and
  1683. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1684. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1685. (RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1))) then
  1686. begin
  1687. if taicpu(hp1).opcode=A_ADD then
  1688. begin
  1689. taicpu(hp1).opcode:=A_MLA;
  1690. if taicpu(hp1).ops=3 then
  1691. begin
  1692. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  1693. oldreg:=taicpu(hp1).oper[2]^.reg
  1694. else
  1695. oldreg:=taicpu(hp1).oper[1]^.reg;
  1696. end
  1697. else
  1698. oldreg:=taicpu(hp1).oper[0]^.reg;
  1699. taicpu(hp1).loadreg(1,taicpu(p).oper[1]^.reg);
  1700. taicpu(hp1).loadreg(2,taicpu(p).oper[2]^.reg);
  1701. taicpu(hp1).loadreg(3,oldreg);
  1702. DebugMsg('MulAdd2MLA done', p);
  1703. taicpu(hp1).ops:=4;
  1704. asml.remove(p);
  1705. p.free;
  1706. p:=hp1;
  1707. end
  1708. else
  1709. begin
  1710. taicpu(hp1).opcode:=A_MLS;
  1711. taicpu(hp1).loadreg(3,taicpu(hp1).oper[1]^.reg);
  1712. if taicpu(hp1).ops=2 then
  1713. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg)
  1714. else
  1715. taicpu(hp1).loadreg(1,taicpu(p).oper[2]^.reg);
  1716. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  1717. DebugMsg('MulSub2MLS done', p);
  1718. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,UsedRegs);
  1719. AllocRegBetween(taicpu(hp1).oper[2]^.reg,p,hp1,UsedRegs);
  1720. AllocRegBetween(taicpu(hp1).oper[3]^.reg,p,hp1,UsedRegs);
  1721. taicpu(hp1).ops:=4;
  1722. RemoveCurrentP(p);
  1723. p:=hp1;
  1724. end;
  1725. result:=true;
  1726. end
  1727. end;
  1728. {$ifdef dummy}
  1729. A_MVN:
  1730. begin
  1731. {
  1732. change
  1733. mvn reg2,reg1
  1734. and reg3,reg4,reg2
  1735. dealloc reg2
  1736. to
  1737. bic reg3,reg4,reg1
  1738. }
  1739. if (taicpu(p).oper[1]^.typ = top_reg) and
  1740. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1741. MatchInstruction(hp1,A_AND,[],[]) and
  1742. (((taicpu(hp1).ops=3) and
  1743. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1744. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1745. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1746. ((taicpu(hp1).ops=2) and
  1747. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1748. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1749. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1750. { reg1 might not be modified inbetween }
  1751. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1752. begin
  1753. DebugMsg('Peephole MvnAnd2Bic done', p);
  1754. taicpu(hp1).opcode:=A_BIC;
  1755. if taicpu(hp1).ops=3 then
  1756. begin
  1757. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1758. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1759. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1760. end
  1761. else
  1762. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1763. GetNextInstruction(p, hp1);
  1764. asml.remove(p);
  1765. p.free;
  1766. p:=hp1;
  1767. end;
  1768. end;
  1769. {$endif dummy}
  1770. A_UXTB:
  1771. Result:=OptPass1UXTB(p);
  1772. A_UXTH:
  1773. begin
  1774. {
  1775. change
  1776. uxth reg2,reg1
  1777. strh reg2,[...]
  1778. dealloc reg2
  1779. to
  1780. strh reg1,[...]
  1781. }
  1782. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1783. (taicpu(p).ops=2) and
  1784. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1785. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1786. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1787. { the reference in strb might not use reg2 }
  1788. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1789. { reg1 might not be modified inbetween }
  1790. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1791. begin
  1792. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1793. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1794. GetNextInstruction(p, hp1);
  1795. asml.remove(p);
  1796. p.free;
  1797. p:=hp1;
  1798. result:=true;
  1799. end
  1800. {
  1801. change
  1802. uxth reg2,reg1
  1803. uxth reg3,reg2
  1804. dealloc reg2
  1805. to
  1806. uxth reg3,reg1
  1807. }
  1808. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1809. (taicpu(p).ops=2) and
  1810. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1811. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1812. (taicpu(hp1).ops=2) and
  1813. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1814. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1815. { reg1 might not be modified inbetween }
  1816. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1817. begin
  1818. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1819. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
  1820. taicpu(hp1).opcode:=A_UXTH;
  1821. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1822. GetNextInstruction(p, hp1);
  1823. asml.remove(p);
  1824. p.free;
  1825. p:=hp1;
  1826. result:=true;
  1827. end
  1828. {
  1829. change
  1830. uxth reg2,reg1
  1831. and reg3,reg2,#65535
  1832. dealloc reg2
  1833. to
  1834. uxth reg3,reg1
  1835. }
  1836. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1837. (taicpu(p).ops=2) and
  1838. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1839. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1840. (taicpu(hp1).ops=3) and
  1841. (taicpu(hp1).oper[2]^.typ=top_const) and
  1842. ((taicpu(hp1).oper[2]^.val and $FFFF)=$FFFF) and
  1843. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1844. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1845. { reg1 might not be modified inbetween }
  1846. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1847. begin
  1848. DebugMsg('Peephole UxthAndImm2Uxth done', p);
  1849. taicpu(hp1).opcode:=A_UXTH;
  1850. taicpu(hp1).ops:=2;
  1851. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1852. GetNextInstruction(p, hp1);
  1853. asml.remove(p);
  1854. p.free;
  1855. p:=hp1;
  1856. result:=true;
  1857. end
  1858. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1859. RemoveSuperfluousMove(p, hp1, 'UxthMov2Data') then
  1860. Result:=true;
  1861. end;
  1862. A_SXTB:
  1863. begin
  1864. {
  1865. change
  1866. sxtb reg2,reg1
  1867. strb reg2,[...]
  1868. dealloc reg2
  1869. to
  1870. strb reg1,[...]
  1871. }
  1872. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1873. (taicpu(p).ops=2) and
  1874. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1875. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1876. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1877. { the reference in strb might not use reg2 }
  1878. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1879. { reg1 might not be modified inbetween }
  1880. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1881. begin
  1882. DebugMsg('Peephole SxtbStrb2Strb done', p);
  1883. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1884. GetNextInstruction(p,hp2);
  1885. asml.remove(p);
  1886. p.free;
  1887. p:=hp2;
  1888. result:=true;
  1889. end
  1890. {
  1891. change
  1892. sxtb reg2,reg1
  1893. sxth reg3,reg2
  1894. dealloc reg2
  1895. to
  1896. sxtb reg3,reg1
  1897. }
  1898. else if MatchInstruction(p, A_SXTB, [C_None], [PF_None]) and
  1899. (taicpu(p).ops=2) and
  1900. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1901. MatchInstruction(hp1, A_SXTH, [C_None], [PF_None]) and
  1902. (taicpu(hp1).ops = 2) and
  1903. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1904. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1905. { reg1 might not be modified inbetween }
  1906. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1907. begin
  1908. DebugMsg('Peephole SxtbSxth2Sxtb done', p);
  1909. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  1910. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1911. asml.remove(hp1);
  1912. hp1.free;
  1913. result:=true;
  1914. end
  1915. {
  1916. change
  1917. sxtb reg2,reg1
  1918. sxtb reg3,reg2
  1919. dealloc reg2
  1920. to
  1921. uxtb reg3,reg1
  1922. }
  1923. else if MatchInstruction(p, A_SXTB, [C_None], [PF_None]) and
  1924. (taicpu(p).ops=2) and
  1925. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1926. MatchInstruction(hp1, A_SXTB, [C_None], [PF_None]) and
  1927. (taicpu(hp1).ops = 2) and
  1928. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1929. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1930. { reg1 might not be modified inbetween }
  1931. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1932. begin
  1933. DebugMsg('Peephole SxtbSxtb2Sxtb done', p);
  1934. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  1935. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1936. asml.remove(hp1);
  1937. hp1.free;
  1938. result:=true;
  1939. end
  1940. {
  1941. change
  1942. sxtb reg2,reg1
  1943. and reg3,reg2,#0x*FF
  1944. dealloc reg2
  1945. to
  1946. uxtb reg3,reg1
  1947. }
  1948. else if MatchInstruction(p, A_SXTB, [C_None], [PF_None]) and
  1949. (taicpu(p).ops=2) and
  1950. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1951. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1952. (taicpu(hp1).ops=3) and
  1953. (taicpu(hp1).oper[2]^.typ=top_const) and
  1954. ((taicpu(hp1).oper[2]^.val and $FF)=$FF) and
  1955. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1956. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1957. { reg1 might not be modified inbetween }
  1958. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1959. begin
  1960. DebugMsg('Peephole SxtbAndImm2Sxtb done', p);
  1961. taicpu(hp1).opcode:=A_SXTB;
  1962. taicpu(hp1).ops:=2;
  1963. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1964. GetNextInstruction(p,hp2);
  1965. asml.remove(p);
  1966. p.free;
  1967. p:=hp2;
  1968. result:=true;
  1969. end
  1970. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1971. RemoveSuperfluousMove(p, hp1, 'SxtbMov2Data') then
  1972. Result:=true;
  1973. end;
  1974. A_SXTH:
  1975. begin
  1976. {
  1977. change
  1978. sxth reg2,reg1
  1979. strh reg2,[...]
  1980. dealloc reg2
  1981. to
  1982. strh reg1,[...]
  1983. }
  1984. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1985. (taicpu(p).ops=2) and
  1986. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1987. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1988. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1989. { the reference in strb might not use reg2 }
  1990. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1991. { reg1 might not be modified inbetween }
  1992. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1993. begin
  1994. DebugMsg('Peephole SXTHStrh2Strh done', p);
  1995. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1996. GetNextInstruction(p, hp1);
  1997. asml.remove(p);
  1998. p.free;
  1999. p:=hp1;
  2000. result:=true;
  2001. end
  2002. {
  2003. change
  2004. sxth reg2,reg1
  2005. sxth reg3,reg2
  2006. dealloc reg2
  2007. to
  2008. sxth reg3,reg1
  2009. }
  2010. else if MatchInstruction(p, A_SXTH, [C_None], [PF_None]) and
  2011. (taicpu(p).ops=2) and
  2012. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  2013. MatchInstruction(hp1, A_SXTH, [C_None], [PF_None]) and
  2014. (taicpu(hp1).ops=2) and
  2015. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  2016. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  2017. { reg1 might not be modified inbetween }
  2018. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  2019. begin
  2020. DebugMsg('Peephole SxthSxth2Sxth done', p);
  2021. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
  2022. taicpu(hp1).opcode:=A_SXTH;
  2023. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  2024. GetNextInstruction(p, hp1);
  2025. asml.remove(p);
  2026. p.free;
  2027. p:=hp1;
  2028. result:=true;
  2029. end
  2030. {
  2031. change
  2032. sxth reg2,reg1
  2033. and reg3,reg2,#65535
  2034. dealloc reg2
  2035. to
  2036. sxth reg3,reg1
  2037. }
  2038. else if MatchInstruction(p, A_SXTH, [C_None], [PF_None]) and
  2039. (taicpu(p).ops=2) and
  2040. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  2041. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  2042. (taicpu(hp1).ops=3) and
  2043. (taicpu(hp1).oper[2]^.typ=top_const) and
  2044. ((taicpu(hp1).oper[2]^.val and $FFFF)=$FFFF) and
  2045. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  2046. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  2047. { reg1 might not be modified inbetween }
  2048. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  2049. begin
  2050. DebugMsg('Peephole SxthAndImm2Sxth done', p);
  2051. taicpu(hp1).opcode:=A_SXTH;
  2052. taicpu(hp1).ops:=2;
  2053. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  2054. GetNextInstruction(p, hp1);
  2055. asml.remove(p);
  2056. p.free;
  2057. p:=hp1;
  2058. result:=true;
  2059. end
  2060. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  2061. RemoveSuperfluousMove(p, hp1, 'SxthMov2Data') then
  2062. Result:=true;
  2063. end;
  2064. A_CMP:
  2065. begin
  2066. {
  2067. change
  2068. cmp reg,const1
  2069. moveq reg,const1
  2070. movne reg,const2
  2071. to
  2072. cmp reg,const1
  2073. movne reg,const2
  2074. }
  2075. if (taicpu(p).oper[1]^.typ = top_const) and
  2076. GetNextInstruction(p, hp1) and
  2077. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  2078. (taicpu(hp1).oper[1]^.typ = top_const) and
  2079. GetNextInstruction(hp1, hp2) and
  2080. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  2081. (taicpu(hp1).oper[1]^.typ = top_const) then
  2082. begin
  2083. Result:=RemoveRedundantMove(p, hp1, asml) or Result;
  2084. Result:=RemoveRedundantMove(p, hp2, asml) or Result;
  2085. end;
  2086. end;
  2087. A_STM:
  2088. begin
  2089. {
  2090. change
  2091. stmfd r13!,[r14]
  2092. sub r13,r13,#4
  2093. bl abc
  2094. add r13,r13,#4
  2095. ldmfd r13!,[r15]
  2096. into
  2097. b abc
  2098. }
  2099. if not(ts_thumb_interworking in current_settings.targetswitches) and
  2100. MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  2101. GetNextInstruction(p, hp1) and
  2102. GetNextInstruction(hp1, hp2) and
  2103. SkipEntryExitMarker(hp2, hp2) and
  2104. GetNextInstruction(hp2, hp3) and
  2105. SkipEntryExitMarker(hp3, hp3) and
  2106. GetNextInstruction(hp3, hp4) and
  2107. (taicpu(p).oper[0]^.typ = top_ref) and
  2108. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2109. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  2110. (taicpu(p).oper[0]^.ref^.offset=0) and
  2111. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2112. (taicpu(p).oper[1]^.typ = top_regset) and
  2113. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  2114. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  2115. (taicpu(hp1).oper[0]^.typ = top_reg) and
  2116. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  2117. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) and
  2118. (taicpu(hp1).oper[2]^.typ = top_const) and
  2119. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  2120. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[0]^) and
  2121. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[1]^) and
  2122. MatchOperand(taicpu(hp1).oper[2]^,taicpu(hp3).oper[2]^) and
  2123. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  2124. (taicpu(hp2).oper[0]^.typ = top_ref) and
  2125. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  2126. MatchOperand(taicpu(p).oper[0]^,taicpu(hp4).oper[0]^) and
  2127. (taicpu(hp4).oper[1]^.typ = top_regset) and
  2128. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  2129. begin
  2130. asml.Remove(p);
  2131. asml.Remove(hp1);
  2132. asml.Remove(hp3);
  2133. asml.Remove(hp4);
  2134. taicpu(hp2).opcode:=A_B;
  2135. p.free;
  2136. hp1.free;
  2137. hp3.free;
  2138. hp4.free;
  2139. p:=hp2;
  2140. DebugMsg('Peephole Bl2B done', p);
  2141. end;
  2142. end;
  2143. A_VMOV:
  2144. begin
  2145. {
  2146. change
  2147. vmov reg0,reg1,reg2
  2148. vmov reg1,reg2,reg0
  2149. into
  2150. vmov reg0,reg1,reg2
  2151. can be applied regardless if reg0 or reg2 is the vfp register
  2152. }
  2153. if (taicpu(p).ops = 3) and
  2154. GetNextInstruction(p, hp1) and
  2155. MatchInstruction(hp1, A_VMOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  2156. (taicpu(hp1).ops = 3) and
  2157. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[2]^) and
  2158. MatchOperand(taicpu(p).oper[1]^, taicpu(hp1).oper[0]^) and
  2159. MatchOperand(taicpu(p).oper[2]^, taicpu(hp1).oper[1]^) then
  2160. begin
  2161. asml.Remove(hp1);
  2162. hp1.free;
  2163. DebugMsg('Peephole VMovVMov2VMov done', p);
  2164. end;
  2165. end;
  2166. A_VLDR,
  2167. A_VADD,
  2168. A_VMUL,
  2169. A_VDIV,
  2170. A_VSUB,
  2171. A_VSQRT,
  2172. A_VNEG,
  2173. A_VCVT,
  2174. A_VABS:
  2175. begin
  2176. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  2177. RemoveSuperfluousVMov(p, hp1, 'VOpVMov2VOp') then
  2178. Result:=true;
  2179. end
  2180. else
  2181. ;
  2182. end;
  2183. end;
  2184. else
  2185. ;
  2186. end;
  2187. end;
  2188. { instructions modifying the CPSR can be only the last instruction }
  2189. function MustBeLast(p : tai) : boolean;
  2190. begin
  2191. Result:=(p.typ=ait_instruction) and
  2192. ((taicpu(p).opcode in [A_BL,A_BLX,A_CMP,A_CMN,A_SWI,A_TEQ,A_TST,A_CMF,A_CMFE {,A_MSR}]) or
  2193. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  2194. (taicpu(p).oppostfix=PF_S));
  2195. end;
  2196. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  2197. var
  2198. p,hp1,hp2: tai;
  2199. l : longint;
  2200. condition : tasmcond;
  2201. hp3: tai;
  2202. WasLast: boolean;
  2203. { UsedRegs, TmpUsedRegs: TRegSet; }
  2204. begin
  2205. p := BlockStart;
  2206. { UsedRegs := []; }
  2207. while (p <> BlockEnd) Do
  2208. begin
  2209. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2210. case p.Typ Of
  2211. Ait_Instruction:
  2212. begin
  2213. case taicpu(p).opcode Of
  2214. A_B:
  2215. if (taicpu(p).condition<>C_None) and
  2216. not(GenerateThumbCode) then
  2217. begin
  2218. { check for
  2219. Bxx xxx
  2220. <several instructions>
  2221. xxx:
  2222. }
  2223. l:=0;
  2224. WasLast:=False;
  2225. GetNextInstruction(p, hp1);
  2226. while assigned(hp1) and
  2227. (l<=4) and
  2228. CanBeCond(hp1) and
  2229. { stop on labels }
  2230. not(hp1.typ=ait_label) and
  2231. { avoid that we cannot recognize the case BccB2Cond }
  2232. not((hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_B)) do
  2233. begin
  2234. inc(l);
  2235. if MustBeLast(hp1) then
  2236. begin
  2237. WasLast:=True;
  2238. GetNextInstruction(hp1,hp1);
  2239. break;
  2240. end
  2241. else
  2242. GetNextInstruction(hp1,hp1);
  2243. end;
  2244. if assigned(hp1) then
  2245. begin
  2246. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2247. begin
  2248. if (l<=4) and (l>0) then
  2249. begin
  2250. condition:=inverse_cond(taicpu(p).condition);
  2251. hp2:=p;
  2252. GetNextInstruction(p,hp1);
  2253. p:=hp1;
  2254. repeat
  2255. if hp1.typ=ait_instruction then
  2256. taicpu(hp1).condition:=condition;
  2257. if MustBeLast(hp1) then
  2258. begin
  2259. GetNextInstruction(hp1,hp1);
  2260. break;
  2261. end
  2262. else
  2263. GetNextInstruction(hp1,hp1);
  2264. until not(assigned(hp1)) or
  2265. not(CanBeCond(hp1)) or
  2266. (hp1.typ=ait_label);
  2267. DebugMsg('Peephole Bcc2Cond done',hp2);
  2268. { wait with removing else GetNextInstruction could
  2269. ignore the label if it was the only usage in the
  2270. jump moved away }
  2271. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2272. asml.remove(hp2);
  2273. hp2.free;
  2274. continue;
  2275. end;
  2276. end
  2277. else
  2278. { do not perform further optimizations if there is inctructon
  2279. in block #1 which can not be optimized.
  2280. }
  2281. if not WasLast then
  2282. begin
  2283. { check further for
  2284. Bcc xxx
  2285. <several instructions 1>
  2286. B yyy
  2287. xxx:
  2288. <several instructions 2>
  2289. yyy:
  2290. }
  2291. { hp2 points to jmp yyy }
  2292. hp2:=hp1;
  2293. { skip hp1 to xxx }
  2294. GetNextInstruction(hp1, hp1);
  2295. if assigned(hp2) and
  2296. assigned(hp1) and
  2297. (l<=3) and
  2298. (hp2.typ=ait_instruction) and
  2299. (taicpu(hp2).is_jmp) and
  2300. (taicpu(hp2).condition=C_None) and
  2301. { real label and jump, no further references to the
  2302. label are allowed }
  2303. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=1) and
  2304. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2305. begin
  2306. l:=0;
  2307. { skip hp1 to <several moves 2> }
  2308. GetNextInstruction(hp1, hp1);
  2309. while assigned(hp1) and
  2310. CanBeCond(hp1) and
  2311. (l<=3) do
  2312. begin
  2313. inc(l);
  2314. if MustBeLast(hp1) then
  2315. begin
  2316. GetNextInstruction(hp1, hp1);
  2317. break;
  2318. end
  2319. else
  2320. GetNextInstruction(hp1, hp1);
  2321. end;
  2322. { hp1 points to yyy: }
  2323. if assigned(hp1) and
  2324. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2325. begin
  2326. condition:=inverse_cond(taicpu(p).condition);
  2327. GetNextInstruction(p,hp1);
  2328. hp3:=p;
  2329. p:=hp1;
  2330. repeat
  2331. if hp1.typ=ait_instruction then
  2332. taicpu(hp1).condition:=condition;
  2333. if MustBeLast(hp1) then
  2334. begin
  2335. GetNextInstruction(hp1, hp1);
  2336. break;
  2337. end
  2338. else
  2339. GetNextInstruction(hp1, hp1);
  2340. until not(assigned(hp1)) or
  2341. not(CanBeCond(hp1)) or
  2342. ((hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_B));
  2343. { hp2 is still at jmp yyy }
  2344. GetNextInstruction(hp2,hp1);
  2345. { hp1 is now at xxx: }
  2346. condition:=inverse_cond(condition);
  2347. GetNextInstruction(hp1,hp1);
  2348. { hp1 is now at <several movs 2> }
  2349. repeat
  2350. if hp1.typ=ait_instruction then
  2351. taicpu(hp1).condition:=condition;
  2352. GetNextInstruction(hp1,hp1);
  2353. until not(assigned(hp1)) or
  2354. not(CanBeCond(hp1)) or
  2355. (hp1.typ=ait_label);
  2356. DebugMsg('Peephole BccB2Cond done',hp3);
  2357. { remove Bcc }
  2358. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  2359. asml.remove(hp3);
  2360. hp3.free;
  2361. { remove B }
  2362. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2363. asml.remove(hp2);
  2364. hp2.free;
  2365. continue;
  2366. end;
  2367. end;
  2368. end;
  2369. end;
  2370. end;
  2371. else
  2372. ;
  2373. end;
  2374. end;
  2375. else
  2376. ;
  2377. end;
  2378. p := tai(p.next)
  2379. end;
  2380. end;
  2381. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  2382. begin
  2383. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  2384. Result:=true
  2385. else If MatchInstruction(p1, [A_LDR, A_STR], [], [PF_D]) and
  2386. (getsupreg(taicpu(p1).oper[0]^.reg)+1=getsupreg(reg)) then
  2387. Result:=true
  2388. else
  2389. Result:=inherited RegInInstruction(Reg, p1);
  2390. end;
  2391. const
  2392. { set of opcode which might or do write to memory }
  2393. { TODO : extend armins.dat to contain r/w info }
  2394. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  2395. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD,A_VSTR,A_VSTM];
  2396. { adjust the register live information when swapping the two instructions p and hp1,
  2397. they must follow one after the other }
  2398. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  2399. procedure CheckLiveEnd(reg : tregister);
  2400. var
  2401. supreg : TSuperRegister;
  2402. regtype : TRegisterType;
  2403. begin
  2404. if reg=NR_NO then
  2405. exit;
  2406. regtype:=getregtype(reg);
  2407. supreg:=getsupreg(reg);
  2408. if assigned(cg.rg[regtype]) and (cg.rg[regtype].live_end[supreg]=hp1) and
  2409. RegInInstruction(reg,p) then
  2410. cg.rg[regtype].live_end[supreg]:=p;
  2411. end;
  2412. procedure CheckLiveStart(reg : TRegister);
  2413. var
  2414. supreg : TSuperRegister;
  2415. regtype : TRegisterType;
  2416. begin
  2417. if reg=NR_NO then
  2418. exit;
  2419. regtype:=getregtype(reg);
  2420. supreg:=getsupreg(reg);
  2421. if assigned(cg.rg[regtype]) and (cg.rg[regtype].live_start[supreg]=p) and
  2422. RegInInstruction(reg,hp1) then
  2423. cg.rg[regtype].live_start[supreg]:=hp1;
  2424. end;
  2425. var
  2426. i : longint;
  2427. r : TSuperRegister;
  2428. begin
  2429. { assumption: p is directly followed by hp1 }
  2430. { if live of any reg used by p starts at p and hp1 uses this register then
  2431. set live start to hp1 }
  2432. for i:=0 to p.ops-1 do
  2433. case p.oper[i]^.typ of
  2434. Top_Reg:
  2435. CheckLiveStart(p.oper[i]^.reg);
  2436. Top_Ref:
  2437. begin
  2438. CheckLiveStart(p.oper[i]^.ref^.base);
  2439. CheckLiveStart(p.oper[i]^.ref^.index);
  2440. end;
  2441. Top_Shifterop:
  2442. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  2443. Top_RegSet:
  2444. for r:=RS_R0 to RS_R15 do
  2445. if r in p.oper[i]^.regset^ then
  2446. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2447. else
  2448. ;
  2449. end;
  2450. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  2451. set live end to p }
  2452. for i:=0 to hp1.ops-1 do
  2453. case hp1.oper[i]^.typ of
  2454. Top_Reg:
  2455. CheckLiveEnd(hp1.oper[i]^.reg);
  2456. Top_Ref:
  2457. begin
  2458. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  2459. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  2460. end;
  2461. Top_Shifterop:
  2462. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  2463. Top_RegSet:
  2464. for r:=RS_R0 to RS_R15 do
  2465. if r in hp1.oper[i]^.regset^ then
  2466. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2467. else
  2468. ;
  2469. end;
  2470. end;
  2471. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  2472. { TODO : schedule also forward }
  2473. { TODO : schedule distance > 1 }
  2474. { returns true if p might be a load of a pc relative tls offset }
  2475. function PossibleTLSLoad(const p: tai) : boolean;
  2476. begin
  2477. Result:=(p.typ=ait_instruction) and (taicpu(p).opcode=A_LDR) and (taicpu(p).oper[1]^.typ=top_ref) and (((taicpu(p).oper[1]^.ref^.base=NR_PC) and
  2478. (taicpu(p).oper[1]^.ref^.index<>NR_NO)) or ((taicpu(p).oper[1]^.ref^.base<>NR_NO) and
  2479. (taicpu(p).oper[1]^.ref^.index=NR_PC)));
  2480. end;
  2481. var
  2482. hp1,hp2,hp3,hp4,hp5,insertpos : tai;
  2483. list : TAsmList;
  2484. begin
  2485. result:=true;
  2486. list:=TAsmList.create;
  2487. p:=BlockStart;
  2488. while p<>BlockEnd Do
  2489. begin
  2490. if (p.typ=ait_instruction) and
  2491. GetNextInstruction(p,hp1) and
  2492. (hp1.typ=ait_instruction) and
  2493. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  2494. (taicpu(hp1).oppostfix in [PF_NONE, PF_B, PF_H, PF_SB, PF_SH]) and
  2495. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  2496. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  2497. not(RegModifiedByInstruction(NR_PC,p))
  2498. ) or
  2499. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  2500. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  2501. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  2502. (taicpu(hp1).oper[1]^.ref^.offset=0)
  2503. )
  2504. ) or
  2505. { try to prove that the memory accesses don't overlapp }
  2506. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  2507. (taicpu(p).oper[1]^.typ = top_ref) and
  2508. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  2509. (taicpu(p).oppostfix=PF_None) and
  2510. (taicpu(hp1).oppostfix=PF_None) and
  2511. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  2512. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  2513. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  2514. (abs(taicpu(p).oper[1]^.ref^.offset-taicpu(hp1).oper[1]^.ref^.offset)>=max(tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)],tcgsize2size[reg_cgsize(taicpu(hp1).oper[0]^.reg)]))
  2515. )
  2516. )
  2517. ) and
  2518. GetNextInstruction(hp1,hp2) and
  2519. (hp2.typ=ait_instruction) and
  2520. { loaded register used by next instruction?
  2521. if we ever support labels (they could be skipped in theory) here, the gnu2 tls general-dynamic code could get broken (the ldr before
  2522. the bl may not be scheduled away from the bl) and it needs to be taken care of this case
  2523. }
  2524. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  2525. { loaded register not used by previous instruction? }
  2526. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  2527. { same condition? }
  2528. (taicpu(p).condition=taicpu(hp1).condition) and
  2529. { first instruction might not change the register used as base }
  2530. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  2531. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  2532. ) and
  2533. { first instruction might not change the register used as index }
  2534. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  2535. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  2536. ) and
  2537. { if we modify the basereg AND the first instruction used that reg, we can not schedule }
  2538. ((taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) or
  2539. not(instructionLoadsFromReg(taicpu(hp1).oper[1]^.ref^.base,p))) and
  2540. not(PossibleTLSLoad(p)) and
  2541. not(PossibleTLSLoad(hp1)) then
  2542. begin
  2543. hp3:=tai(p.Previous);
  2544. hp5:=tai(p.next);
  2545. asml.Remove(p);
  2546. { if there is a reg. alloc/dealloc/sync instructions or address labels (e.g. for GOT-less PIC)
  2547. associated with p, move it together with p }
  2548. { before the instruction? }
  2549. { find reg allocs,deallocs and PIC labels }
  2550. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  2551. begin
  2552. if ( (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_alloc, ra_dealloc]) and
  2553. RegInInstruction(tai_regalloc(hp3).reg,p) )
  2554. or ( (hp3.typ=ait_label) and (tai_label(hp3).labsym.typ=AT_ADDR) )
  2555. then
  2556. begin
  2557. hp4:=hp3;
  2558. hp3:=tai(hp3.Previous);
  2559. asml.Remove(hp4);
  2560. list.Insert(hp4);
  2561. end
  2562. else
  2563. hp3:=tai(hp3.Previous);
  2564. end;
  2565. list.Concat(p);
  2566. SwapRegLive(taicpu(p),taicpu(hp1));
  2567. { after the instruction? }
  2568. { find reg deallocs and reg syncs }
  2569. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  2570. begin
  2571. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc, ra_sync]) and
  2572. RegInInstruction(tai_regalloc(hp5).reg,p) then
  2573. begin
  2574. hp4:=hp5;
  2575. hp5:=tai(hp5.next);
  2576. asml.Remove(hp4);
  2577. list.Concat(hp4);
  2578. end
  2579. else
  2580. hp5:=tai(hp5.Next);
  2581. end;
  2582. asml.Remove(hp1);
  2583. { if there are address labels associated with hp2, those must
  2584. stay with hp2 (e.g. for GOT-less PIC) }
  2585. insertpos:=hp2;
  2586. while assigned(hp2.previous) and
  2587. (tai(hp2.previous).typ<>ait_instruction) do
  2588. begin
  2589. hp2:=tai(hp2.previous);
  2590. if (hp2.typ=ait_label) and
  2591. (tai_label(hp2).labsym.typ=AT_ADDR) then
  2592. insertpos:=hp2;
  2593. end;
  2594. {$ifdef DEBUG_PREREGSCHEDULER}
  2595. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),insertpos);
  2596. {$endif DEBUG_PREREGSCHEDULER}
  2597. asml.InsertBefore(hp1,insertpos);
  2598. asml.InsertListBefore(insertpos,list);
  2599. p:=tai(p.next);
  2600. end
  2601. else if p.typ=ait_instruction then
  2602. p:=hp1
  2603. else
  2604. p:=tai(p.next);
  2605. end;
  2606. list.Free;
  2607. end;
  2608. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  2609. var
  2610. hp : tai;
  2611. l : longint;
  2612. begin
  2613. hp := tai(p.Previous);
  2614. l := 1;
  2615. while assigned(hp) and
  2616. (l <= 4) do
  2617. begin
  2618. if hp.typ=ait_instruction then
  2619. begin
  2620. if (taicpu(hp).opcode>=A_IT) and
  2621. (taicpu(hp).opcode <= A_ITTTT) then
  2622. begin
  2623. if (taicpu(hp).opcode = A_IT) and
  2624. (l=1) then
  2625. list.Remove(hp)
  2626. else
  2627. case taicpu(hp).opcode of
  2628. A_ITE:
  2629. if l=2 then taicpu(hp).opcode := A_IT;
  2630. A_ITT:
  2631. if l=2 then taicpu(hp).opcode := A_IT;
  2632. A_ITEE:
  2633. if l=3 then taicpu(hp).opcode := A_ITE;
  2634. A_ITTE:
  2635. if l=3 then taicpu(hp).opcode := A_ITT;
  2636. A_ITET:
  2637. if l=3 then taicpu(hp).opcode := A_ITE;
  2638. A_ITTT:
  2639. if l=3 then taicpu(hp).opcode := A_ITT;
  2640. A_ITEEE:
  2641. if l=4 then taicpu(hp).opcode := A_ITEE;
  2642. A_ITTEE:
  2643. if l=4 then taicpu(hp).opcode := A_ITTE;
  2644. A_ITETE:
  2645. if l=4 then taicpu(hp).opcode := A_ITET;
  2646. A_ITTTE:
  2647. if l=4 then taicpu(hp).opcode := A_ITTT;
  2648. A_ITEET:
  2649. if l=4 then taicpu(hp).opcode := A_ITEE;
  2650. A_ITTET:
  2651. if l=4 then taicpu(hp).opcode := A_ITTE;
  2652. A_ITETT:
  2653. if l=4 then taicpu(hp).opcode := A_ITET;
  2654. A_ITTTT:
  2655. begin
  2656. if l=4 then taicpu(hp).opcode := A_ITTT;
  2657. end
  2658. else
  2659. ;
  2660. end;
  2661. break;
  2662. end;
  2663. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  2664. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  2665. break;}
  2666. inc(l);
  2667. end;
  2668. hp := tai(hp.Previous);
  2669. end;
  2670. end;
  2671. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  2672. var
  2673. hp : taicpu;
  2674. //hp1,hp2 : tai;
  2675. begin
  2676. result:=false;
  2677. if inherited PeepHoleOptPass1Cpu(p) then
  2678. result:=true
  2679. else if (p.typ=ait_instruction) and
  2680. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  2681. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2682. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2683. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  2684. begin
  2685. DebugMsg('Peephole Stm2Push done', p);
  2686. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2687. AsmL.InsertAfter(hp, p);
  2688. asml.Remove(p);
  2689. p:=hp;
  2690. result:=true;
  2691. end
  2692. {else if (p.typ=ait_instruction) and
  2693. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  2694. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  2695. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2696. (taicpu(p).oper[1]^.ref^.offset=-4) and
  2697. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  2698. begin
  2699. DebugMsg('Peephole Str2Push done', p);
  2700. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2701. asml.InsertAfter(hp, p);
  2702. asml.Remove(p);
  2703. p.Free;
  2704. p:=hp;
  2705. result:=true;
  2706. end}
  2707. else if (p.typ=ait_instruction) and
  2708. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  2709. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2710. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2711. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  2712. begin
  2713. DebugMsg('Peephole Ldm2Pop done', p);
  2714. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2715. asml.InsertBefore(hp, p);
  2716. asml.Remove(p);
  2717. p.Free;
  2718. p:=hp;
  2719. result:=true;
  2720. end
  2721. {else if (p.typ=ait_instruction) and
  2722. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  2723. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  2724. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2725. (taicpu(p).oper[1]^.ref^.offset=4) and
  2726. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  2727. begin
  2728. DebugMsg('Peephole Ldr2Pop done', p);
  2729. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2730. asml.InsertBefore(hp, p);
  2731. asml.Remove(p);
  2732. p.Free;
  2733. p:=hp;
  2734. result:=true;
  2735. end}
  2736. else if (p.typ=ait_instruction) and
  2737. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2738. (taicpu(p).ops = 2) and
  2739. (taicpu(p).oper[1]^.typ=top_const) and
  2740. ((taicpu(p).oper[1]^.val=255) or
  2741. (taicpu(p).oper[1]^.val=65535)) then
  2742. begin
  2743. DebugMsg('Peephole AndR2Uxt done', p);
  2744. if taicpu(p).oper[1]^.val=255 then
  2745. taicpu(p).opcode:=A_UXTB
  2746. else
  2747. taicpu(p).opcode:=A_UXTH;
  2748. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  2749. result := true;
  2750. end
  2751. else if (p.typ=ait_instruction) and
  2752. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2753. (taicpu(p).ops = 3) and
  2754. (taicpu(p).oper[2]^.typ=top_const) and
  2755. ((taicpu(p).oper[2]^.val=255) or
  2756. (taicpu(p).oper[2]^.val=65535)) then
  2757. begin
  2758. DebugMsg('Peephole AndRR2Uxt done', p);
  2759. if taicpu(p).oper[2]^.val=255 then
  2760. taicpu(p).opcode:=A_UXTB
  2761. else
  2762. taicpu(p).opcode:=A_UXTH;
  2763. taicpu(p).ops:=2;
  2764. result := true;
  2765. end
  2766. {else if (p.typ=ait_instruction) and
  2767. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  2768. (taicpu(p).oper[1]^.typ=top_const) and
  2769. (taicpu(p).oper[1]^.val=0) and
  2770. GetNextInstruction(p,hp1) and
  2771. (taicpu(hp1).opcode=A_B) and
  2772. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  2773. begin
  2774. if taicpu(hp1).condition = C_EQ then
  2775. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  2776. else
  2777. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  2778. taicpu(hp2).is_jmp := true;
  2779. asml.InsertAfter(hp2, hp1);
  2780. asml.Remove(hp1);
  2781. hp1.Free;
  2782. asml.Remove(p);
  2783. p.Free;
  2784. p := hp2;
  2785. result := true;
  2786. end}
  2787. end;
  2788. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2789. var
  2790. p,hp1,hp2: tai;
  2791. l : longint;
  2792. condition : tasmcond;
  2793. { UsedRegs, TmpUsedRegs: TRegSet; }
  2794. begin
  2795. p := BlockStart;
  2796. { UsedRegs := []; }
  2797. while (p <> BlockEnd) Do
  2798. begin
  2799. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2800. case p.Typ Of
  2801. Ait_Instruction:
  2802. begin
  2803. case taicpu(p).opcode Of
  2804. A_B:
  2805. if taicpu(p).condition<>C_None then
  2806. begin
  2807. { check for
  2808. Bxx xxx
  2809. <several instructions>
  2810. xxx:
  2811. }
  2812. l:=0;
  2813. GetNextInstruction(p, hp1);
  2814. while assigned(hp1) and
  2815. (l<=4) and
  2816. CanBeCond(hp1) and
  2817. { stop on labels }
  2818. not(hp1.typ=ait_label) do
  2819. begin
  2820. inc(l);
  2821. if MustBeLast(hp1) then
  2822. begin
  2823. //hp1:=nil;
  2824. GetNextInstruction(hp1,hp1);
  2825. break;
  2826. end
  2827. else
  2828. GetNextInstruction(hp1,hp1);
  2829. end;
  2830. if assigned(hp1) then
  2831. begin
  2832. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2833. begin
  2834. if (l<=4) and (l>0) then
  2835. begin
  2836. condition:=inverse_cond(taicpu(p).condition);
  2837. hp2:=p;
  2838. GetNextInstruction(p,hp1);
  2839. p:=hp1;
  2840. repeat
  2841. if hp1.typ=ait_instruction then
  2842. taicpu(hp1).condition:=condition;
  2843. if MustBeLast(hp1) then
  2844. begin
  2845. GetNextInstruction(hp1,hp1);
  2846. break;
  2847. end
  2848. else
  2849. GetNextInstruction(hp1,hp1);
  2850. until not(assigned(hp1)) or
  2851. not(CanBeCond(hp1)) or
  2852. (hp1.typ=ait_label);
  2853. { wait with removing else GetNextInstruction could
  2854. ignore the label if it was the only usage in the
  2855. jump moved away }
  2856. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2857. DecrementPreceedingIT(asml, hp2);
  2858. case l of
  2859. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2860. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2861. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2862. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2863. end;
  2864. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2865. asml.remove(hp2);
  2866. hp2.free;
  2867. continue;
  2868. end;
  2869. end;
  2870. end;
  2871. end;
  2872. else
  2873. ;
  2874. end;
  2875. end;
  2876. else
  2877. ;
  2878. end;
  2879. p := tai(p.next)
  2880. end;
  2881. end;
  2882. function TCpuThumb2AsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  2883. begin
  2884. result:=false;
  2885. if p.typ = ait_instruction then
  2886. begin
  2887. if MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  2888. (taicpu(p).oper[1]^.typ=top_const) and
  2889. (taicpu(p).oper[1]^.val >= 0) and
  2890. (taicpu(p).oper[1]^.val < 256) and
  2891. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2892. begin
  2893. DebugMsg('Peephole Mov2Movs done', p);
  2894. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2895. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2896. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2897. taicpu(p).oppostfix:=PF_S;
  2898. result:=true;
  2899. end
  2900. else if MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  2901. (taicpu(p).oper[1]^.typ=top_reg) and
  2902. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2903. begin
  2904. DebugMsg('Peephole Mvn2Mvns done', p);
  2905. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2906. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2907. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2908. taicpu(p).oppostfix:=PF_S;
  2909. result:=true;
  2910. end
  2911. else if MatchInstruction(p, A_RSB, [C_None], [PF_None]) and
  2912. (taicpu(p).ops = 3) and
  2913. (taicpu(p).oper[2]^.typ=top_const) and
  2914. (taicpu(p).oper[2]^.val=0) and
  2915. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2916. begin
  2917. DebugMsg('Peephole Rsb2Rsbs done', p);
  2918. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2919. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2920. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2921. taicpu(p).oppostfix:=PF_S;
  2922. result:=true;
  2923. end
  2924. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2925. (taicpu(p).ops = 3) and
  2926. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2927. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2928. (taicpu(p).oper[2]^.typ=top_const) and
  2929. (taicpu(p).oper[2]^.val >= 0) and
  2930. (taicpu(p).oper[2]^.val < 256) and
  2931. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2932. begin
  2933. DebugMsg('Peephole AddSub2*s done', p);
  2934. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2935. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2936. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2937. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2938. taicpu(p).oppostfix:=PF_S;
  2939. taicpu(p).ops := 2;
  2940. result:=true;
  2941. end
  2942. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2943. (taicpu(p).ops = 2) and
  2944. (taicpu(p).oper[1]^.typ=top_reg) and
  2945. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2946. (not MatchOperand(taicpu(p).oper[1]^, NR_STACK_POINTER_REG)) and
  2947. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2948. begin
  2949. DebugMsg('Peephole AddSub2*s done', p);
  2950. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2951. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2952. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2953. taicpu(p).oppostfix:=PF_S;
  2954. result:=true;
  2955. end
  2956. else if MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  2957. (taicpu(p).ops = 3) and
  2958. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2959. (taicpu(p).oper[2]^.typ=top_reg) then
  2960. begin
  2961. DebugMsg('Peephole AddRRR2AddRR done', p);
  2962. taicpu(p).ops := 2;
  2963. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2964. result:=true;
  2965. end
  2966. else if MatchInstruction(p, [A_AND,A_ORR,A_EOR,A_BIC,A_LSL,A_LSR,A_ASR,A_ROR], [C_None], [PF_None]) and
  2967. (taicpu(p).ops = 3) and
  2968. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2969. (taicpu(p).oper[2]^.typ=top_reg) and
  2970. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2971. begin
  2972. DebugMsg('Peephole opXXY2opsXY done', p);
  2973. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2974. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2975. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2976. taicpu(p).ops := 2;
  2977. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2978. taicpu(p).oppostfix:=PF_S;
  2979. result:=true;
  2980. end
  2981. else if MatchInstruction(p, [A_AND,A_ORR,A_EOR,A_BIC,A_LSL,A_LSR,A_ASR,A_ROR], [C_None], [PF_S]) and
  2982. (taicpu(p).ops = 3) and
  2983. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2984. (taicpu(p).oper[2]^.typ in [top_reg,top_const]) then
  2985. begin
  2986. DebugMsg('Peephole opXXY2opXY done', p);
  2987. taicpu(p).ops := 2;
  2988. if taicpu(p).oper[2]^.typ=top_reg then
  2989. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg)
  2990. else
  2991. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2992. result:=true;
  2993. end
  2994. else if MatchInstruction(p, [A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  2995. (taicpu(p).ops = 3) and
  2996. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  2997. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2998. begin
  2999. DebugMsg('Peephole opXYX2opsXY done', p);
  3000. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  3001. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  3002. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  3003. taicpu(p).oppostfix:=PF_S;
  3004. taicpu(p).ops := 2;
  3005. result:=true;
  3006. end
  3007. else if MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  3008. (taicpu(p).ops=3) and
  3009. (taicpu(p).oper[2]^.typ=top_shifterop) and
  3010. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  3011. //MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  3012. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  3013. begin
  3014. DebugMsg('Peephole Mov2Shift done', p);
  3015. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  3016. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  3017. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  3018. taicpu(p).oppostfix:=PF_S;
  3019. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  3020. SM_LSL: taicpu(p).opcode:=A_LSL;
  3021. SM_LSR: taicpu(p).opcode:=A_LSR;
  3022. SM_ASR: taicpu(p).opcode:=A_ASR;
  3023. SM_ROR: taicpu(p).opcode:=A_ROR;
  3024. else
  3025. internalerror(2019050912);
  3026. end;
  3027. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  3028. taicpu(p).loadreg(2, taicpu(p).oper[2]^.shifterop^.rs)
  3029. else
  3030. taicpu(p).loadconst(2, taicpu(p).oper[2]^.shifterop^.shiftimm);
  3031. result:=true;
  3032. end
  3033. end;
  3034. end;
  3035. begin
  3036. casmoptimizer:=TCpuAsmOptimizer;
  3037. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  3038. End.