aoptcpu.pas 134 KB

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