aoptcpu.pas 132 KB

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