aoptcpu.pas 144 KB

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