aoptcpu.pas 129 KB

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