aoptcpu.pas 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  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. // Make sure that Thumb code doesn't propagate a high register into a reference
  1120. ((GenerateThumbCode and
  1121. (getsupreg(taicpu(p).oper[1]^.reg) < RS_R8)) or
  1122. (not GenerateThumbCode)) and
  1123. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1124. begin
  1125. DebugMsg('Peephole MovLdr2Ldr done', hp1);
  1126. if (taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) and
  1127. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1128. taicpu(hp1).oper[1]^.ref^.base := taicpu(p).oper[1]^.reg;
  1129. if taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg then
  1130. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1131. GetNextInstruction(p, hp1);
  1132. asml.remove(p);
  1133. p.free;
  1134. p:=hp1;
  1135. result:=true;
  1136. end;
  1137. { This folds shifterops into following instructions
  1138. mov r0, r1, lsl #8
  1139. add r2, r3, r0
  1140. to
  1141. add r2, r3, r1, lsl #8
  1142. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1143. }
  1144. if (taicpu(p).opcode = A_MOV) and
  1145. (taicpu(p).ops = 3) and
  1146. (taicpu(p).oper[1]^.typ = top_reg) and
  1147. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1148. (taicpu(p).oppostfix = PF_NONE) and
  1149. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1150. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1151. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  1152. A_CMP, A_CMN],
  1153. [taicpu(p).condition], [PF_None]) and
  1154. (not ((GenerateThumb2Code) and
  1155. (taicpu(hp1).opcode in [A_SBC]) and
  1156. (((taicpu(hp1).ops=3) and
  1157. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^.reg)) or
  1158. ((taicpu(hp1).ops=2) and
  1159. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg))))) and
  1160. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) and
  1161. (taicpu(hp1).ops >= 2) and
  1162. {Currently we can't fold into another shifterop}
  1163. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  1164. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  1165. NR_DEFAULTFLAGS for modification}
  1166. (
  1167. {Everything is fine if we don't use RRX}
  1168. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  1169. (
  1170. {If it is RRX, then check if we're just accessing the next instruction}
  1171. GetNextInstruction(p, hp2) and
  1172. (hp1 = hp2)
  1173. )
  1174. ) and
  1175. { reg1 might not be modified inbetween }
  1176. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1177. { The shifterop can contain a register, might not be modified}
  1178. (
  1179. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  1180. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  1181. ) and
  1182. (
  1183. {Only ONE of the two src operands is allowed to match}
  1184. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  1185. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  1186. ) then
  1187. begin
  1188. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  1189. I2:=0
  1190. else
  1191. I2:=1;
  1192. for I:=I2 to taicpu(hp1).ops-1 do
  1193. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  1194. begin
  1195. { If the parameter matched on the second op from the RIGHT
  1196. we have to switch the parameters, this will not happen for CMP
  1197. were we're only evaluating the most right parameter
  1198. }
  1199. if I <> taicpu(hp1).ops-1 then
  1200. begin
  1201. {The SUB operators need to be changed when we swap parameters}
  1202. case taicpu(hp1).opcode of
  1203. A_SUB: tempop:=A_RSB;
  1204. A_SBC: tempop:=A_RSC;
  1205. A_RSB: tempop:=A_SUB;
  1206. A_RSC: tempop:=A_SBC;
  1207. else tempop:=taicpu(hp1).opcode;
  1208. end;
  1209. if taicpu(hp1).ops = 3 then
  1210. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1211. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1212. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1213. else
  1214. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1215. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1216. taicpu(p).oper[2]^.shifterop^);
  1217. end
  1218. else
  1219. if taicpu(hp1).ops = 3 then
  1220. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1221. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1222. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1223. else
  1224. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1225. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1226. taicpu(p).oper[2]^.shifterop^);
  1227. asml.insertbefore(hp2, hp1);
  1228. GetNextInstruction(p, hp2);
  1229. asml.remove(p);
  1230. asml.remove(hp1);
  1231. p.free;
  1232. hp1.free;
  1233. p:=hp2;
  1234. DebugMsg('Peephole FoldShiftProcess done', p);
  1235. Result:=true;
  1236. break;
  1237. end;
  1238. end;
  1239. {
  1240. Fold
  1241. mov r1, r1, lsl #2
  1242. ldr/ldrb r0, [r0, r1]
  1243. to
  1244. ldr/ldrb r0, [r0, r1, lsl #2]
  1245. XXX: This still needs some work, as we quite often encounter something like
  1246. mov r1, r2, lsl #2
  1247. add r2, r3, #imm
  1248. ldr r0, [r2, r1]
  1249. which can't be folded because r2 is overwritten between the shift and the ldr.
  1250. We could try to shuffle the registers around and fold it into.
  1251. add r1, r3, #imm
  1252. ldr r0, [r1, r2, lsl #2]
  1253. }
  1254. if (not(GenerateThumbCode)) and
  1255. (taicpu(p).opcode = A_MOV) and
  1256. (taicpu(p).ops = 3) and
  1257. (taicpu(p).oper[1]^.typ = top_reg) and
  1258. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1259. { RRX is tough to handle, because it requires tracking the C-Flag,
  1260. it is also extremly unlikely to be emitted this way}
  1261. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1262. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1263. { thumb2 allows only lsl #0..#3 }
  1264. (not(GenerateThumb2Code) or
  1265. ((taicpu(p).oper[2]^.shifterop^.shiftimm in [0..3]) and
  1266. (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL)
  1267. )
  1268. ) and
  1269. (taicpu(p).oppostfix = PF_NONE) and
  1270. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1271. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1272. (MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B]) or
  1273. (GenerateThumb2Code and
  1274. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B, PF_SB, PF_H, PF_SH]))
  1275. ) and
  1276. (
  1277. {If this is address by offset, one of the two registers can be used}
  1278. ((taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1279. (
  1280. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) xor
  1281. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg)
  1282. )
  1283. ) or
  1284. {For post and preindexed only the index register can be used}
  1285. ((taicpu(hp1).oper[1]^.ref^.addressmode in [AM_POSTINDEXED, AM_PREINDEXED]) and
  1286. (
  1287. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1288. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg)
  1289. ) and
  1290. (not GenerateThumb2Code)
  1291. )
  1292. ) and
  1293. { Only fold if there isn't another shifterop already, and offset is zero. }
  1294. (taicpu(hp1).oper[1]^.ref^.offset = 0) and
  1295. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1296. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1297. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1298. begin
  1299. { If the register we want to do the shift for resides in base, we need to swap that}
  1300. if (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1301. taicpu(hp1).oper[1]^.ref^.base := taicpu(hp1).oper[1]^.ref^.index;
  1302. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1303. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1304. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1305. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1306. GetNextInstruction(p, hp1);
  1307. asml.remove(p);
  1308. p.free;
  1309. p:=hp1;
  1310. Result:=true;
  1311. end;
  1312. {
  1313. Often we see shifts and then a superfluous mov to another register
  1314. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1315. }
  1316. if (taicpu(p).opcode = A_MOV) and
  1317. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1318. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov') then
  1319. Result:=true;
  1320. end;
  1321. A_ADD,
  1322. A_ADC,
  1323. A_RSB,
  1324. A_RSC,
  1325. A_SUB,
  1326. A_SBC,
  1327. A_AND,
  1328. A_BIC,
  1329. A_EOR,
  1330. A_ORR,
  1331. A_MLA,
  1332. A_MLS,
  1333. A_MUL:
  1334. begin
  1335. {
  1336. optimize
  1337. and reg2,reg1,const1
  1338. ...
  1339. }
  1340. if (taicpu(p).opcode = A_AND) and
  1341. (taicpu(p).ops>2) and
  1342. (taicpu(p).oper[1]^.typ = top_reg) and
  1343. (taicpu(p).oper[2]^.typ = top_const) then
  1344. begin
  1345. {
  1346. change
  1347. and reg2,reg1,const1
  1348. ...
  1349. and reg3,reg2,const2
  1350. to
  1351. and reg3,reg1,(const1 and const2)
  1352. }
  1353. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1354. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1355. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1356. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1357. (taicpu(hp1).oper[2]^.typ = top_const) then
  1358. begin
  1359. if not(RegUsedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) then
  1360. begin
  1361. DebugMsg('Peephole AndAnd2And done', p);
  1362. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1363. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1364. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1365. asml.remove(hp1);
  1366. hp1.free;
  1367. Result:=true;
  1368. end
  1369. else if not(RegUsedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1370. begin
  1371. DebugMsg('Peephole AndAnd2And done', hp1);
  1372. taicpu(hp1).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1373. taicpu(hp1).oppostfix:=taicpu(p).oppostfix;
  1374. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1375. GetNextInstruction(p, hp1);
  1376. asml.remove(p);
  1377. p.free;
  1378. p:=hp1;
  1379. Result:=true;
  1380. end;
  1381. end
  1382. {
  1383. change
  1384. and reg2,reg1,$xxxxxxFF
  1385. strb reg2,[...]
  1386. dealloc reg2
  1387. to
  1388. strb reg1,[...]
  1389. }
  1390. else if ((taicpu(p).oper[2]^.val and $FF) = $FF) and
  1391. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1392. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1393. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1394. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1395. { the reference in strb might not use reg2 }
  1396. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1397. { reg1 might not be modified inbetween }
  1398. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1399. begin
  1400. DebugMsg('Peephole AndStrb2Strb done', p);
  1401. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1402. GetNextInstruction(p, hp1);
  1403. asml.remove(p);
  1404. p.free;
  1405. p:=hp1;
  1406. result:=true;
  1407. end
  1408. {
  1409. change
  1410. and reg2,reg1,255
  1411. uxtb/uxth reg3,reg2
  1412. dealloc reg2
  1413. to
  1414. and reg3,reg1,x
  1415. }
  1416. else if (taicpu(p).oper[2]^.val = $FF) and
  1417. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1418. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1419. MatchInstruction(hp1, [A_UXTB,A_UXTH], [C_None], [PF_None]) and
  1420. (taicpu(hp1).ops = 2) and
  1421. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1422. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1423. { reg1 might not be modified inbetween }
  1424. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1425. begin
  1426. DebugMsg('Peephole AndUxt2And done', p);
  1427. taicpu(hp1).opcode:=A_AND;
  1428. taicpu(hp1).ops:=3;
  1429. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1430. taicpu(hp1).loadconst(2,255);
  1431. GetNextInstruction(p,hp1);
  1432. asml.remove(p);
  1433. p.Free;
  1434. p:=hp1;
  1435. result:=true;
  1436. end
  1437. {
  1438. from
  1439. and reg1,reg0,2^n-1
  1440. mov reg2,reg1, lsl imm1
  1441. (mov reg3,reg2, lsr/asr imm1)
  1442. remove either the and or the lsl/xsr sequence if possible
  1443. }
  1444. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1445. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1446. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1447. (taicpu(hp1).ops=3) and
  1448. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1449. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1450. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1451. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1452. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) then
  1453. begin
  1454. {
  1455. and reg1,reg0,2^n-1
  1456. mov reg2,reg1, lsl imm1
  1457. mov reg3,reg2, lsr/asr imm1
  1458. =>
  1459. and reg1,reg0,2^n-1
  1460. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1461. }
  1462. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1463. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1464. (taicpu(hp2).ops=3) and
  1465. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1466. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1467. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1468. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1469. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1470. RegEndOfLife(taicpu(hp1).oper[0]^.reg,taicpu(hp2)) and
  1471. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1472. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1473. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1474. begin
  1475. DebugMsg('Peephole AndLslXsr2And done', p);
  1476. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1477. asml.Remove(hp1);
  1478. asml.Remove(hp2);
  1479. hp1.free;
  1480. hp2.free;
  1481. result:=true;
  1482. end
  1483. {
  1484. and reg1,reg0,2^n-1
  1485. mov reg2,reg1, lsl imm1
  1486. =>
  1487. mov reg2,reg1, lsl imm1
  1488. if imm1>i
  1489. }
  1490. else if i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm then
  1491. begin
  1492. DebugMsg('Peephole AndLsl2Lsl done', p);
  1493. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[0]^.reg;
  1494. GetNextInstruction(p, hp1);
  1495. asml.Remove(p);
  1496. p.free;
  1497. p:=hp1;
  1498. result:=true;
  1499. end
  1500. end;
  1501. end;
  1502. {
  1503. change
  1504. add/sub reg2,reg1,const1
  1505. str/ldr reg3,[reg2,const2]
  1506. dealloc reg2
  1507. to
  1508. str/ldr reg3,[reg1,const2+/-const1]
  1509. }
  1510. if (not GenerateThumbCode) and
  1511. (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1512. (taicpu(p).ops>2) and
  1513. (taicpu(p).oper[1]^.typ = top_reg) and
  1514. (taicpu(p).oper[2]^.typ = top_const) then
  1515. begin
  1516. hp1:=p;
  1517. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1518. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1519. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1520. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1521. { don't optimize if the register is stored/overwritten }
  1522. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1523. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1524. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1525. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1526. ldr postfix }
  1527. (((taicpu(p).opcode=A_ADD) and
  1528. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1529. ) or
  1530. ((taicpu(p).opcode=A_SUB) and
  1531. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1532. )
  1533. ) do
  1534. begin
  1535. { neither reg1 nor reg2 might be changed inbetween }
  1536. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1537. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1538. break;
  1539. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1540. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1541. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1542. begin
  1543. { remember last instruction }
  1544. hp2:=hp1;
  1545. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1546. hp1:=p;
  1547. { fix all ldr/str }
  1548. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1549. begin
  1550. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1551. if taicpu(p).opcode=A_ADD then
  1552. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1553. else
  1554. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1555. if hp1=hp2 then
  1556. break;
  1557. end;
  1558. GetNextInstruction(p,hp1);
  1559. asml.remove(p);
  1560. p.free;
  1561. p:=hp1;
  1562. result:=true;
  1563. break;
  1564. end;
  1565. end;
  1566. end;
  1567. {
  1568. change
  1569. add reg1, ...
  1570. mov reg2, reg1
  1571. to
  1572. add reg2, ...
  1573. }
  1574. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1575. (taicpu(p).ops>=3) and
  1576. RemoveSuperfluousMove(p, hp1, 'DataMov2Data') then
  1577. Result:=true;
  1578. if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1579. LookForPreindexedPattern(taicpu(p)) then
  1580. begin
  1581. GetNextInstruction(p,hp1);
  1582. DebugMsg('Peephole Add/Sub to Preindexed done', p);
  1583. asml.remove(p);
  1584. p.free;
  1585. p:=hp1;
  1586. Result:=true;
  1587. end;
  1588. {
  1589. Turn
  1590. mul reg0, z,w
  1591. sub/add x, y, reg0
  1592. dealloc reg0
  1593. into
  1594. mls/mla x,z,w,y
  1595. }
  1596. if MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  1597. (taicpu(p).ops=3) and
  1598. (taicpu(p).oper[0]^.typ = top_reg) and
  1599. (taicpu(p).oper[1]^.typ = top_reg) and
  1600. (taicpu(p).oper[2]^.typ = top_reg) and
  1601. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1602. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  1603. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  1604. (not RegModifiedBetween(taicpu(p).oper[2]^.reg, p, hp1)) and
  1605. (((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype>=cpu_armv4)) or
  1606. ((taicpu(hp1).opcode=A_SUB) and (current_settings.cputype in [cpu_armv6t2,cpu_armv7,cpu_armv7a,cpu_armv7r,cpu_armv7m,cpu_armv7em]))) and
  1607. // CPUs before ARMv6 don't recommend having the same Rd and Rm for MLA.
  1608. // TODO: A workaround would be to swap Rm and Rs
  1609. (not ((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype<=cpu_armv6) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^))) and
  1610. (((taicpu(hp1).ops=3) and
  1611. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1612. ((MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) and
  1613. (not RegModifiedBetween(taicpu(hp1).oper[1]^.reg, p, hp1))) or
  1614. ((MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1615. (taicpu(hp1).opcode=A_ADD) and
  1616. (not RegModifiedBetween(taicpu(hp1).oper[2]^.reg, p, hp1)))))) or
  1617. ((taicpu(hp1).ops=2) and
  1618. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1619. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1620. (RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1))) then
  1621. begin
  1622. if taicpu(hp1).opcode=A_ADD then
  1623. begin
  1624. taicpu(hp1).opcode:=A_MLA;
  1625. if taicpu(hp1).ops=3 then
  1626. begin
  1627. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  1628. oldreg:=taicpu(hp1).oper[2]^.reg
  1629. else
  1630. oldreg:=taicpu(hp1).oper[1]^.reg;
  1631. end
  1632. else
  1633. oldreg:=taicpu(hp1).oper[0]^.reg;
  1634. taicpu(hp1).loadreg(1,taicpu(p).oper[1]^.reg);
  1635. taicpu(hp1).loadreg(2,taicpu(p).oper[2]^.reg);
  1636. taicpu(hp1).loadreg(3,oldreg);
  1637. DebugMsg('MulAdd2MLA done', p);
  1638. taicpu(hp1).ops:=4;
  1639. asml.remove(p);
  1640. p.free;
  1641. p:=hp1;
  1642. end
  1643. else
  1644. begin
  1645. taicpu(hp1).opcode:=A_MLS;
  1646. taicpu(hp1).loadreg(3,taicpu(hp1).oper[1]^.reg);
  1647. if taicpu(hp1).ops=2 then
  1648. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg)
  1649. else
  1650. taicpu(hp1).loadreg(1,taicpu(p).oper[2]^.reg);
  1651. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  1652. DebugMsg('MulSub2MLS done', p);
  1653. taicpu(hp1).ops:=4;
  1654. asml.remove(p);
  1655. p.free;
  1656. p:=hp1;
  1657. end;
  1658. result:=true;
  1659. end
  1660. end;
  1661. {$ifdef dummy}
  1662. A_MVN:
  1663. begin
  1664. {
  1665. change
  1666. mvn reg2,reg1
  1667. and reg3,reg4,reg2
  1668. dealloc reg2
  1669. to
  1670. bic reg3,reg4,reg1
  1671. }
  1672. if (taicpu(p).oper[1]^.typ = top_reg) and
  1673. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1674. MatchInstruction(hp1,A_AND,[],[]) and
  1675. (((taicpu(hp1).ops=3) and
  1676. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1677. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1678. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1679. ((taicpu(hp1).ops=2) and
  1680. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1681. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1682. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1683. { reg1 might not be modified inbetween }
  1684. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1685. begin
  1686. DebugMsg('Peephole MvnAnd2Bic done', p);
  1687. taicpu(hp1).opcode:=A_BIC;
  1688. if taicpu(hp1).ops=3 then
  1689. begin
  1690. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1691. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1692. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1693. end
  1694. else
  1695. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1696. GetNextInstruction(p, hp1);
  1697. asml.remove(p);
  1698. p.free;
  1699. p:=hp1;
  1700. end;
  1701. end;
  1702. {$endif dummy}
  1703. A_UXTB:
  1704. begin
  1705. {
  1706. change
  1707. uxtb reg2,reg1
  1708. strb reg2,[...]
  1709. dealloc reg2
  1710. to
  1711. strb reg1,[...]
  1712. }
  1713. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1714. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1715. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1716. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1717. { the reference in strb might not use reg2 }
  1718. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1719. { reg1 might not be modified inbetween }
  1720. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1721. begin
  1722. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1723. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1724. GetNextInstruction(p,hp2);
  1725. asml.remove(p);
  1726. p.free;
  1727. p:=hp2;
  1728. result:=true;
  1729. end
  1730. {
  1731. change
  1732. uxtb reg2,reg1
  1733. uxth reg3,reg2
  1734. dealloc reg2
  1735. to
  1736. uxtb reg3,reg1
  1737. }
  1738. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1739. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1740. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1741. (taicpu(hp1).ops = 2) and
  1742. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1743. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1744. { reg1 might not be modified inbetween }
  1745. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1746. begin
  1747. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1748. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1749. asml.remove(hp1);
  1750. hp1.free;
  1751. result:=true;
  1752. end
  1753. {
  1754. change
  1755. uxtb reg2,reg1
  1756. uxtb reg3,reg2
  1757. dealloc reg2
  1758. to
  1759. uxtb reg3,reg1
  1760. }
  1761. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1762. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1763. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1764. (taicpu(hp1).ops = 2) and
  1765. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1766. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1767. { reg1 might not be modified inbetween }
  1768. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1769. begin
  1770. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1771. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1772. asml.remove(hp1);
  1773. hp1.free;
  1774. result:=true;
  1775. end
  1776. {
  1777. change
  1778. uxtb reg2,reg1
  1779. and reg3,reg2,#0x*FF
  1780. dealloc reg2
  1781. to
  1782. uxtb reg3,reg1
  1783. }
  1784. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1785. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1786. (taicpu(p).ops=2) and
  1787. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1788. (taicpu(hp1).ops=3) and
  1789. (taicpu(hp1).oper[2]^.typ=top_const) and
  1790. ((taicpu(hp1).oper[2]^.val and $FF)=$FF) and
  1791. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1792. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1793. { reg1 might not be modified inbetween }
  1794. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1795. begin
  1796. DebugMsg('Peephole UxtbAndImm2Uxtb done', p);
  1797. taicpu(hp1).opcode:=A_UXTB;
  1798. taicpu(hp1).ops:=2;
  1799. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1800. GetNextInstruction(p,hp2);
  1801. asml.remove(p);
  1802. p.free;
  1803. p:=hp2;
  1804. result:=true;
  1805. end
  1806. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1807. RemoveSuperfluousMove(p, hp1, 'UxtbMov2Data') then
  1808. Result:=true;
  1809. end;
  1810. A_UXTH:
  1811. begin
  1812. {
  1813. change
  1814. uxth reg2,reg1
  1815. strh reg2,[...]
  1816. dealloc reg2
  1817. to
  1818. strh reg1,[...]
  1819. }
  1820. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1821. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1822. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1823. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1824. { the reference in strb might not use reg2 }
  1825. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1826. { reg1 might not be modified inbetween }
  1827. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1828. begin
  1829. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1830. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1831. GetNextInstruction(p, hp1);
  1832. asml.remove(p);
  1833. p.free;
  1834. p:=hp1;
  1835. result:=true;
  1836. end
  1837. {
  1838. change
  1839. uxth reg2,reg1
  1840. uxth reg3,reg2
  1841. dealloc reg2
  1842. to
  1843. uxth reg3,reg1
  1844. }
  1845. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1846. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1847. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1848. (taicpu(hp1).ops=2) and
  1849. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1850. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1851. { reg1 might not be modified inbetween }
  1852. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1853. begin
  1854. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1855. taicpu(hp1).opcode:=A_UXTH;
  1856. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1857. GetNextInstruction(p, hp1);
  1858. asml.remove(p);
  1859. p.free;
  1860. p:=hp1;
  1861. result:=true;
  1862. end
  1863. {
  1864. change
  1865. uxth reg2,reg1
  1866. and reg3,reg2,#65535
  1867. dealloc reg2
  1868. to
  1869. uxth reg3,reg1
  1870. }
  1871. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1872. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1873. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1874. (taicpu(hp1).ops=3) and
  1875. (taicpu(hp1).oper[2]^.typ=top_const) and
  1876. ((taicpu(hp1).oper[2]^.val and $FFFF)=$FFFF) and
  1877. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1878. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1879. { reg1 might not be modified inbetween }
  1880. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1881. begin
  1882. DebugMsg('Peephole UxthAndImm2Uxth done', p);
  1883. taicpu(hp1).opcode:=A_UXTH;
  1884. taicpu(hp1).ops:=2;
  1885. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1886. GetNextInstruction(p, hp1);
  1887. asml.remove(p);
  1888. p.free;
  1889. p:=hp1;
  1890. result:=true;
  1891. end
  1892. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1893. RemoveSuperfluousMove(p, hp1, 'UxthMov2Data') then
  1894. Result:=true;
  1895. end;
  1896. A_CMP:
  1897. begin
  1898. {
  1899. change
  1900. cmp reg,const1
  1901. moveq reg,const1
  1902. movne reg,const2
  1903. to
  1904. cmp reg,const1
  1905. movne reg,const2
  1906. }
  1907. if (taicpu(p).oper[1]^.typ = top_const) and
  1908. GetNextInstruction(p, hp1) and
  1909. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1910. (taicpu(hp1).oper[1]^.typ = top_const) and
  1911. GetNextInstruction(hp1, hp2) and
  1912. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1913. (taicpu(hp1).oper[1]^.typ = top_const) then
  1914. begin
  1915. Result:=RemoveRedundantMove(p, hp1, asml) or Result;
  1916. Result:=RemoveRedundantMove(p, hp2, asml) or Result;
  1917. end;
  1918. end;
  1919. A_STM:
  1920. begin
  1921. {
  1922. change
  1923. stmfd r13!,[r14]
  1924. sub r13,r13,#4
  1925. bl abc
  1926. add r13,r13,#4
  1927. ldmfd r13!,[r15]
  1928. into
  1929. b abc
  1930. }
  1931. if not(ts_thumb_interworking in current_settings.targetswitches) and
  1932. MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  1933. GetNextInstruction(p, hp1) and
  1934. GetNextInstruction(hp1, hp2) and
  1935. SkipEntryExitMarker(hp2, hp2) and
  1936. GetNextInstruction(hp2, hp3) and
  1937. SkipEntryExitMarker(hp3, hp3) and
  1938. GetNextInstruction(hp3, hp4) and
  1939. (taicpu(p).oper[0]^.typ = top_ref) and
  1940. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1941. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  1942. (taicpu(p).oper[0]^.ref^.offset=0) and
  1943. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1944. (taicpu(p).oper[1]^.typ = top_regset) and
  1945. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  1946. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  1947. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1948. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1949. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) and
  1950. (taicpu(hp1).oper[2]^.typ = top_const) and
  1951. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  1952. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[0]^) and
  1953. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[1]^) and
  1954. MatchOperand(taicpu(hp1).oper[2]^,taicpu(hp3).oper[2]^) and
  1955. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  1956. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1957. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  1958. MatchOperand(taicpu(p).oper[0]^,taicpu(hp4).oper[0]^) and
  1959. (taicpu(hp4).oper[1]^.typ = top_regset) and
  1960. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  1961. begin
  1962. asml.Remove(p);
  1963. asml.Remove(hp1);
  1964. asml.Remove(hp3);
  1965. asml.Remove(hp4);
  1966. taicpu(hp2).opcode:=A_B;
  1967. p.free;
  1968. hp1.free;
  1969. hp3.free;
  1970. hp4.free;
  1971. p:=hp2;
  1972. DebugMsg('Peephole Bl2B done', p);
  1973. end;
  1974. end;
  1975. end;
  1976. end;
  1977. end;
  1978. end;
  1979. { instructions modifying the CPSR can be only the last instruction }
  1980. function MustBeLast(p : tai) : boolean;
  1981. begin
  1982. Result:=(p.typ=ait_instruction) and
  1983. ((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
  1984. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1985. (taicpu(p).oppostfix=PF_S));
  1986. end;
  1987. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1988. var
  1989. p,hp1,hp2: tai;
  1990. l : longint;
  1991. condition : tasmcond;
  1992. hp3: tai;
  1993. WasLast: boolean;
  1994. { UsedRegs, TmpUsedRegs: TRegSet; }
  1995. begin
  1996. p := BlockStart;
  1997. { UsedRegs := []; }
  1998. while (p <> BlockEnd) Do
  1999. begin
  2000. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2001. case p.Typ Of
  2002. Ait_Instruction:
  2003. begin
  2004. case taicpu(p).opcode Of
  2005. A_B:
  2006. if (taicpu(p).condition<>C_None) and
  2007. not(GenerateThumbCode) then
  2008. begin
  2009. { check for
  2010. Bxx xxx
  2011. <several instructions>
  2012. xxx:
  2013. }
  2014. l:=0;
  2015. WasLast:=False;
  2016. GetNextInstruction(p, hp1);
  2017. while assigned(hp1) and
  2018. (l<=4) and
  2019. CanBeCond(hp1) and
  2020. { stop on labels }
  2021. not(hp1.typ=ait_label) do
  2022. begin
  2023. inc(l);
  2024. if MustBeLast(hp1) then
  2025. begin
  2026. WasLast:=True;
  2027. GetNextInstruction(hp1,hp1);
  2028. break;
  2029. end
  2030. else
  2031. GetNextInstruction(hp1,hp1);
  2032. end;
  2033. if assigned(hp1) then
  2034. begin
  2035. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2036. begin
  2037. if (l<=4) and (l>0) then
  2038. begin
  2039. condition:=inverse_cond(taicpu(p).condition);
  2040. hp2:=p;
  2041. GetNextInstruction(p,hp1);
  2042. p:=hp1;
  2043. repeat
  2044. if hp1.typ=ait_instruction then
  2045. taicpu(hp1).condition:=condition;
  2046. if MustBeLast(hp1) then
  2047. begin
  2048. GetNextInstruction(hp1,hp1);
  2049. break;
  2050. end
  2051. else
  2052. GetNextInstruction(hp1,hp1);
  2053. until not(assigned(hp1)) or
  2054. not(CanBeCond(hp1)) or
  2055. (hp1.typ=ait_label);
  2056. { wait with removing else GetNextInstruction could
  2057. ignore the label if it was the only usage in the
  2058. jump moved away }
  2059. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2060. asml.remove(hp2);
  2061. hp2.free;
  2062. continue;
  2063. end;
  2064. end
  2065. else
  2066. { do not perform further optimizations if there is inctructon
  2067. in block #1 which can not be optimized.
  2068. }
  2069. if not WasLast then
  2070. begin
  2071. { check further for
  2072. Bcc xxx
  2073. <several instructions 1>
  2074. B yyy
  2075. xxx:
  2076. <several instructions 2>
  2077. yyy:
  2078. }
  2079. { hp2 points to jmp yyy }
  2080. hp2:=hp1;
  2081. { skip hp1 to xxx }
  2082. GetNextInstruction(hp1, hp1);
  2083. if assigned(hp2) and
  2084. assigned(hp1) and
  2085. (l<=3) and
  2086. (hp2.typ=ait_instruction) and
  2087. (taicpu(hp2).is_jmp) and
  2088. (taicpu(hp2).condition=C_None) and
  2089. { real label and jump, no further references to the
  2090. label are allowed }
  2091. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  2092. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2093. begin
  2094. l:=0;
  2095. { skip hp1 to <several moves 2> }
  2096. GetNextInstruction(hp1, hp1);
  2097. while assigned(hp1) and
  2098. CanBeCond(hp1) do
  2099. begin
  2100. inc(l);
  2101. GetNextInstruction(hp1, hp1);
  2102. end;
  2103. { hp1 points to yyy: }
  2104. if assigned(hp1) and
  2105. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2106. begin
  2107. condition:=inverse_cond(taicpu(p).condition);
  2108. GetNextInstruction(p,hp1);
  2109. hp3:=p;
  2110. p:=hp1;
  2111. repeat
  2112. if hp1.typ=ait_instruction then
  2113. taicpu(hp1).condition:=condition;
  2114. GetNextInstruction(hp1,hp1);
  2115. until not(assigned(hp1)) or
  2116. not(CanBeCond(hp1));
  2117. { hp2 is still at jmp yyy }
  2118. GetNextInstruction(hp2,hp1);
  2119. { hp2 is now at xxx: }
  2120. condition:=inverse_cond(condition);
  2121. GetNextInstruction(hp1,hp1);
  2122. { hp1 is now at <several movs 2> }
  2123. repeat
  2124. taicpu(hp1).condition:=condition;
  2125. GetNextInstruction(hp1,hp1);
  2126. until not(assigned(hp1)) or
  2127. not(CanBeCond(hp1)) or
  2128. (hp1.typ=ait_label);
  2129. {
  2130. asml.remove(hp1.next)
  2131. hp1.next.free;
  2132. asml.remove(hp1);
  2133. hp1.free;
  2134. }
  2135. { remove Bcc }
  2136. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  2137. asml.remove(hp3);
  2138. hp3.free;
  2139. { remove jmp }
  2140. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2141. asml.remove(hp2);
  2142. hp2.free;
  2143. continue;
  2144. end;
  2145. end;
  2146. end;
  2147. end;
  2148. end;
  2149. end;
  2150. end;
  2151. end;
  2152. p := tai(p.next)
  2153. end;
  2154. end;
  2155. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  2156. begin
  2157. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  2158. Result:=true
  2159. else If MatchInstruction(p1, [A_LDR, A_STR], [], [PF_D]) and
  2160. (getsupreg(taicpu(p1).oper[0]^.reg)+1=getsupreg(reg)) then
  2161. Result:=true
  2162. else
  2163. Result:=inherited RegInInstruction(Reg, p1);
  2164. end;
  2165. const
  2166. { set of opcode which might or do write to memory }
  2167. { TODO : extend armins.dat to contain r/w info }
  2168. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  2169. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  2170. { adjust the register live information when swapping the two instructions p and hp1,
  2171. they must follow one after the other }
  2172. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  2173. procedure CheckLiveEnd(reg : tregister);
  2174. var
  2175. supreg : TSuperRegister;
  2176. regtype : TRegisterType;
  2177. begin
  2178. if reg=NR_NO then
  2179. exit;
  2180. regtype:=getregtype(reg);
  2181. supreg:=getsupreg(reg);
  2182. if (cg.rg[regtype].live_end[supreg]=hp1) and
  2183. RegInInstruction(reg,p) then
  2184. cg.rg[regtype].live_end[supreg]:=p;
  2185. end;
  2186. procedure CheckLiveStart(reg : TRegister);
  2187. var
  2188. supreg : TSuperRegister;
  2189. regtype : TRegisterType;
  2190. begin
  2191. if reg=NR_NO then
  2192. exit;
  2193. regtype:=getregtype(reg);
  2194. supreg:=getsupreg(reg);
  2195. if (cg.rg[regtype].live_start[supreg]=p) and
  2196. RegInInstruction(reg,hp1) then
  2197. cg.rg[regtype].live_start[supreg]:=hp1;
  2198. end;
  2199. var
  2200. i : longint;
  2201. r : TSuperRegister;
  2202. begin
  2203. { assumption: p is directly followed by hp1 }
  2204. { if live of any reg used by p starts at p and hp1 uses this register then
  2205. set live start to hp1 }
  2206. for i:=0 to p.ops-1 do
  2207. case p.oper[i]^.typ of
  2208. Top_Reg:
  2209. CheckLiveStart(p.oper[i]^.reg);
  2210. Top_Ref:
  2211. begin
  2212. CheckLiveStart(p.oper[i]^.ref^.base);
  2213. CheckLiveStart(p.oper[i]^.ref^.index);
  2214. end;
  2215. Top_Shifterop:
  2216. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  2217. Top_RegSet:
  2218. for r:=RS_R0 to RS_R15 do
  2219. if r in p.oper[i]^.regset^ then
  2220. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2221. end;
  2222. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  2223. set live end to p }
  2224. for i:=0 to hp1.ops-1 do
  2225. case hp1.oper[i]^.typ of
  2226. Top_Reg:
  2227. CheckLiveEnd(hp1.oper[i]^.reg);
  2228. Top_Ref:
  2229. begin
  2230. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  2231. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  2232. end;
  2233. Top_Shifterop:
  2234. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  2235. Top_RegSet:
  2236. for r:=RS_R0 to RS_R15 do
  2237. if r in hp1.oper[i]^.regset^ then
  2238. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2239. end;
  2240. end;
  2241. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  2242. { TODO : schedule also forward }
  2243. { TODO : schedule distance > 1 }
  2244. var
  2245. hp1,hp2,hp3,hp4,hp5,insertpos : tai;
  2246. list : TAsmList;
  2247. begin
  2248. result:=true;
  2249. list:=TAsmList.create_without_marker;
  2250. p:=BlockStart;
  2251. while p<>BlockEnd Do
  2252. begin
  2253. if (p.typ=ait_instruction) and
  2254. GetNextInstruction(p,hp1) and
  2255. (hp1.typ=ait_instruction) and
  2256. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  2257. (taicpu(hp1).oppostfix in [PF_NONE, PF_B, PF_H, PF_SB, PF_SH]) and
  2258. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  2259. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  2260. not(RegModifiedByInstruction(NR_PC,p))
  2261. ) or
  2262. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  2263. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  2264. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  2265. (taicpu(hp1).oper[1]^.ref^.offset=0)
  2266. )
  2267. ) or
  2268. { try to prove that the memory accesses don't overlapp }
  2269. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  2270. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  2271. (taicpu(p).oppostfix=PF_None) and
  2272. (taicpu(hp1).oppostfix=PF_None) and
  2273. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  2274. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  2275. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  2276. (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)]))
  2277. )
  2278. )
  2279. ) and
  2280. GetNextInstruction(hp1,hp2) and
  2281. (hp2.typ=ait_instruction) and
  2282. { loaded register used by next instruction? }
  2283. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  2284. { loaded register not used by previous instruction? }
  2285. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  2286. { same condition? }
  2287. (taicpu(p).condition=taicpu(hp1).condition) and
  2288. { first instruction might not change the register used as base }
  2289. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  2290. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  2291. ) and
  2292. { first instruction might not change the register used as index }
  2293. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  2294. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  2295. ) then
  2296. begin
  2297. hp3:=tai(p.Previous);
  2298. hp5:=tai(p.next);
  2299. asml.Remove(p);
  2300. { if there is a reg. dealloc instruction associated with p, move it together with p }
  2301. { before the instruction? }
  2302. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  2303. begin
  2304. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  2305. RegInInstruction(tai_regalloc(hp3).reg,p) then
  2306. begin
  2307. hp4:=hp3;
  2308. hp3:=tai(hp3.Previous);
  2309. asml.Remove(hp4);
  2310. list.Concat(hp4);
  2311. end
  2312. else
  2313. hp3:=tai(hp3.Previous);
  2314. end;
  2315. list.Concat(p);
  2316. SwapRegLive(taicpu(p),taicpu(hp1));
  2317. { after the instruction? }
  2318. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  2319. begin
  2320. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  2321. RegInInstruction(tai_regalloc(hp5).reg,p) then
  2322. begin
  2323. hp4:=hp5;
  2324. hp5:=tai(hp5.next);
  2325. asml.Remove(hp4);
  2326. list.Concat(hp4);
  2327. end
  2328. else
  2329. hp5:=tai(hp5.Next);
  2330. end;
  2331. asml.Remove(hp1);
  2332. { if there are address labels associated with hp2, those must
  2333. stay with hp2 (e.g. for GOT-less PIC) }
  2334. insertpos:=hp2;
  2335. while assigned(hp2.previous) and
  2336. (tai(hp2.previous).typ<>ait_instruction) do
  2337. begin
  2338. hp2:=tai(hp2.previous);
  2339. if (hp2.typ=ait_label) and
  2340. (tai_label(hp2).labsym.typ=AT_ADDR) then
  2341. insertpos:=hp2;
  2342. end;
  2343. {$ifdef DEBUG_PREREGSCHEDULER}
  2344. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),insertpos);
  2345. {$endif DEBUG_PREREGSCHEDULER}
  2346. asml.InsertBefore(hp1,insertpos);
  2347. asml.InsertListBefore(insertpos,list);
  2348. p:=tai(p.next)
  2349. end
  2350. else if p.typ=ait_instruction then
  2351. p:=hp1
  2352. else
  2353. p:=tai(p.next);
  2354. end;
  2355. list.Free;
  2356. end;
  2357. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  2358. var
  2359. hp : tai;
  2360. l : longint;
  2361. begin
  2362. hp := tai(p.Previous);
  2363. l := 1;
  2364. while assigned(hp) and
  2365. (l <= 4) do
  2366. begin
  2367. if hp.typ=ait_instruction then
  2368. begin
  2369. if (taicpu(hp).opcode>=A_IT) and
  2370. (taicpu(hp).opcode <= A_ITTTT) then
  2371. begin
  2372. if (taicpu(hp).opcode = A_IT) and
  2373. (l=1) then
  2374. list.Remove(hp)
  2375. else
  2376. case taicpu(hp).opcode of
  2377. A_ITE:
  2378. if l=2 then taicpu(hp).opcode := A_IT;
  2379. A_ITT:
  2380. if l=2 then taicpu(hp).opcode := A_IT;
  2381. A_ITEE:
  2382. if l=3 then taicpu(hp).opcode := A_ITE;
  2383. A_ITTE:
  2384. if l=3 then taicpu(hp).opcode := A_ITT;
  2385. A_ITET:
  2386. if l=3 then taicpu(hp).opcode := A_ITE;
  2387. A_ITTT:
  2388. if l=3 then taicpu(hp).opcode := A_ITT;
  2389. A_ITEEE:
  2390. if l=4 then taicpu(hp).opcode := A_ITEE;
  2391. A_ITTEE:
  2392. if l=4 then taicpu(hp).opcode := A_ITTE;
  2393. A_ITETE:
  2394. if l=4 then taicpu(hp).opcode := A_ITET;
  2395. A_ITTTE:
  2396. if l=4 then taicpu(hp).opcode := A_ITTT;
  2397. A_ITEET:
  2398. if l=4 then taicpu(hp).opcode := A_ITEE;
  2399. A_ITTET:
  2400. if l=4 then taicpu(hp).opcode := A_ITTE;
  2401. A_ITETT:
  2402. if l=4 then taicpu(hp).opcode := A_ITET;
  2403. A_ITTTT:
  2404. if l=4 then taicpu(hp).opcode := A_ITTT;
  2405. end;
  2406. break;
  2407. end;
  2408. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  2409. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  2410. break;}
  2411. inc(l);
  2412. end;
  2413. hp := tai(hp.Previous);
  2414. end;
  2415. end;
  2416. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  2417. var
  2418. hp : taicpu;
  2419. hp1,hp2 : tai;
  2420. oldreg : TRegister;
  2421. begin
  2422. result:=false;
  2423. if inherited PeepHoleOptPass1Cpu(p) then
  2424. result:=true
  2425. else if (p.typ=ait_instruction) and
  2426. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  2427. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2428. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2429. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  2430. begin
  2431. DebugMsg('Peephole Stm2Push done', p);
  2432. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2433. AsmL.InsertAfter(hp, p);
  2434. asml.Remove(p);
  2435. p:=hp;
  2436. result:=true;
  2437. end
  2438. {else if (p.typ=ait_instruction) and
  2439. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  2440. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  2441. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2442. (taicpu(p).oper[1]^.ref^.offset=-4) and
  2443. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  2444. begin
  2445. DebugMsg('Peephole Str2Push done', p);
  2446. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2447. asml.InsertAfter(hp, p);
  2448. asml.Remove(p);
  2449. p.Free;
  2450. p:=hp;
  2451. result:=true;
  2452. end}
  2453. else if (p.typ=ait_instruction) and
  2454. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  2455. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2456. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2457. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  2458. begin
  2459. DebugMsg('Peephole Ldm2Pop done', p);
  2460. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2461. asml.InsertBefore(hp, p);
  2462. asml.Remove(p);
  2463. p.Free;
  2464. p:=hp;
  2465. result:=true;
  2466. end
  2467. {else if (p.typ=ait_instruction) and
  2468. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  2469. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  2470. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2471. (taicpu(p).oper[1]^.ref^.offset=4) and
  2472. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  2473. begin
  2474. DebugMsg('Peephole Ldr2Pop done', p);
  2475. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2476. asml.InsertBefore(hp, p);
  2477. asml.Remove(p);
  2478. p.Free;
  2479. p:=hp;
  2480. result:=true;
  2481. end}
  2482. else if (p.typ=ait_instruction) and
  2483. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2484. (taicpu(p).ops = 2) and
  2485. (taicpu(p).oper[1]^.typ=top_const) and
  2486. ((taicpu(p).oper[1]^.val=255) or
  2487. (taicpu(p).oper[1]^.val=65535)) then
  2488. begin
  2489. DebugMsg('Peephole AndR2Uxt done', p);
  2490. if taicpu(p).oper[1]^.val=255 then
  2491. taicpu(p).opcode:=A_UXTB
  2492. else
  2493. taicpu(p).opcode:=A_UXTH;
  2494. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  2495. result := true;
  2496. end
  2497. else if (p.typ=ait_instruction) and
  2498. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2499. (taicpu(p).ops = 3) and
  2500. (taicpu(p).oper[2]^.typ=top_const) and
  2501. ((taicpu(p).oper[2]^.val=255) or
  2502. (taicpu(p).oper[2]^.val=65535)) then
  2503. begin
  2504. DebugMsg('Peephole AndRR2Uxt done', p);
  2505. if taicpu(p).oper[2]^.val=255 then
  2506. taicpu(p).opcode:=A_UXTB
  2507. else
  2508. taicpu(p).opcode:=A_UXTH;
  2509. taicpu(p).ops:=2;
  2510. result := true;
  2511. end
  2512. {else if (p.typ=ait_instruction) and
  2513. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  2514. (taicpu(p).oper[1]^.typ=top_const) and
  2515. (taicpu(p).oper[1]^.val=0) and
  2516. GetNextInstruction(p,hp1) and
  2517. (taicpu(hp1).opcode=A_B) and
  2518. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  2519. begin
  2520. if taicpu(hp1).condition = C_EQ then
  2521. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  2522. else
  2523. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  2524. taicpu(hp2).is_jmp := true;
  2525. asml.InsertAfter(hp2, hp1);
  2526. asml.Remove(hp1);
  2527. hp1.Free;
  2528. asml.Remove(p);
  2529. p.Free;
  2530. p := hp2;
  2531. result := true;
  2532. end}
  2533. end;
  2534. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2535. var
  2536. p,hp1,hp2: tai;
  2537. l,l2 : longint;
  2538. condition : tasmcond;
  2539. hp3: tai;
  2540. WasLast: boolean;
  2541. { UsedRegs, TmpUsedRegs: TRegSet; }
  2542. begin
  2543. p := BlockStart;
  2544. { UsedRegs := []; }
  2545. while (p <> BlockEnd) Do
  2546. begin
  2547. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2548. case p.Typ Of
  2549. Ait_Instruction:
  2550. begin
  2551. case taicpu(p).opcode Of
  2552. A_B:
  2553. if taicpu(p).condition<>C_None then
  2554. begin
  2555. { check for
  2556. Bxx xxx
  2557. <several instructions>
  2558. xxx:
  2559. }
  2560. l:=0;
  2561. GetNextInstruction(p, hp1);
  2562. while assigned(hp1) and
  2563. (l<=4) and
  2564. CanBeCond(hp1) and
  2565. { stop on labels }
  2566. not(hp1.typ=ait_label) do
  2567. begin
  2568. inc(l);
  2569. if MustBeLast(hp1) then
  2570. begin
  2571. //hp1:=nil;
  2572. GetNextInstruction(hp1,hp1);
  2573. break;
  2574. end
  2575. else
  2576. GetNextInstruction(hp1,hp1);
  2577. end;
  2578. if assigned(hp1) then
  2579. begin
  2580. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2581. begin
  2582. if (l<=4) and (l>0) then
  2583. begin
  2584. condition:=inverse_cond(taicpu(p).condition);
  2585. hp2:=p;
  2586. GetNextInstruction(p,hp1);
  2587. p:=hp1;
  2588. repeat
  2589. if hp1.typ=ait_instruction then
  2590. taicpu(hp1).condition:=condition;
  2591. if MustBeLast(hp1) then
  2592. begin
  2593. GetNextInstruction(hp1,hp1);
  2594. break;
  2595. end
  2596. else
  2597. GetNextInstruction(hp1,hp1);
  2598. until not(assigned(hp1)) or
  2599. not(CanBeCond(hp1)) or
  2600. (hp1.typ=ait_label);
  2601. { wait with removing else GetNextInstruction could
  2602. ignore the label if it was the only usage in the
  2603. jump moved away }
  2604. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2605. DecrementPreceedingIT(asml, hp2);
  2606. case l of
  2607. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2608. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2609. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2610. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2611. end;
  2612. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2613. asml.remove(hp2);
  2614. hp2.free;
  2615. continue;
  2616. end;
  2617. end;
  2618. end;
  2619. end;
  2620. end;
  2621. end;
  2622. end;
  2623. p := tai(p.next)
  2624. end;
  2625. end;
  2626. function TCpuThumb2AsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  2627. begin
  2628. result:=false;
  2629. if p.typ = ait_instruction then
  2630. begin
  2631. if MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  2632. (taicpu(p).oper[1]^.typ=top_const) and
  2633. (taicpu(p).oper[1]^.val >= 0) and
  2634. (taicpu(p).oper[1]^.val < 256) and
  2635. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2636. begin
  2637. DebugMsg('Peephole Mov2Movs done', p);
  2638. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2639. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2640. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2641. taicpu(p).oppostfix:=PF_S;
  2642. result:=true;
  2643. end
  2644. else if MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  2645. (taicpu(p).oper[1]^.typ=top_reg) and
  2646. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2647. begin
  2648. DebugMsg('Peephole Mvn2Mvns done', p);
  2649. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2650. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2651. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2652. taicpu(p).oppostfix:=PF_S;
  2653. result:=true;
  2654. end
  2655. else if MatchInstruction(p, A_RSB, [C_None], [PF_None]) and
  2656. (taicpu(p).ops = 3) and
  2657. (taicpu(p).oper[2]^.typ=top_const) and
  2658. (taicpu(p).oper[2]^.val=0) and
  2659. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2660. begin
  2661. DebugMsg('Peephole Rsb2Rsbs done', p);
  2662. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2663. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2664. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2665. taicpu(p).oppostfix:=PF_S;
  2666. result:=true;
  2667. end
  2668. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2669. (taicpu(p).ops = 3) and
  2670. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2671. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2672. (taicpu(p).oper[2]^.typ=top_const) and
  2673. (taicpu(p).oper[2]^.val >= 0) and
  2674. (taicpu(p).oper[2]^.val < 256) and
  2675. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2676. begin
  2677. DebugMsg('Peephole AddSub2*s done', p);
  2678. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2679. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2680. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2681. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2682. taicpu(p).oppostfix:=PF_S;
  2683. taicpu(p).ops := 2;
  2684. result:=true;
  2685. end
  2686. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2687. (taicpu(p).ops = 2) and
  2688. (taicpu(p).oper[1]^.typ=top_reg) and
  2689. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2690. (not MatchOperand(taicpu(p).oper[1]^, NR_STACK_POINTER_REG)) and
  2691. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2692. begin
  2693. DebugMsg('Peephole AddSub2*s done', p);
  2694. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2695. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2696. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2697. taicpu(p).oppostfix:=PF_S;
  2698. result:=true;
  2699. end
  2700. else if MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  2701. (taicpu(p).ops = 3) and
  2702. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2703. (taicpu(p).oper[2]^.typ=top_reg) then
  2704. begin
  2705. DebugMsg('Peephole AddRRR2AddRR done', p);
  2706. taicpu(p).ops := 2;
  2707. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2708. result:=true;
  2709. end
  2710. 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
  2711. (taicpu(p).ops = 3) and
  2712. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2713. (taicpu(p).oper[2]^.typ=top_reg) and
  2714. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2715. begin
  2716. DebugMsg('Peephole opXXY2opsXY done', p);
  2717. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2718. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2719. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2720. taicpu(p).ops := 2;
  2721. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2722. taicpu(p).oppostfix:=PF_S;
  2723. result:=true;
  2724. end
  2725. 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
  2726. (taicpu(p).ops = 3) and
  2727. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2728. (taicpu(p).oper[2]^.typ in [top_reg,top_const]) then
  2729. begin
  2730. DebugMsg('Peephole opXXY2opXY done', p);
  2731. taicpu(p).ops := 2;
  2732. if taicpu(p).oper[2]^.typ=top_reg then
  2733. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg)
  2734. else
  2735. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2736. result:=true;
  2737. end
  2738. else if MatchInstruction(p, [A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  2739. (taicpu(p).ops = 3) and
  2740. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  2741. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2742. begin
  2743. DebugMsg('Peephole opXYX2opsXY done', p);
  2744. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2745. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2746. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2747. taicpu(p).oppostfix:=PF_S;
  2748. taicpu(p).ops := 2;
  2749. result:=true;
  2750. end
  2751. else if MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  2752. (taicpu(p).ops=3) and
  2753. (taicpu(p).oper[2]^.typ=top_shifterop) and
  2754. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  2755. //MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2756. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2757. begin
  2758. DebugMsg('Peephole Mov2Shift done', p);
  2759. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2760. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2761. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2762. taicpu(p).oppostfix:=PF_S;
  2763. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  2764. SM_LSL: taicpu(p).opcode:=A_LSL;
  2765. SM_LSR: taicpu(p).opcode:=A_LSR;
  2766. SM_ASR: taicpu(p).opcode:=A_ASR;
  2767. SM_ROR: taicpu(p).opcode:=A_ROR;
  2768. end;
  2769. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  2770. taicpu(p).loadreg(2, taicpu(p).oper[2]^.shifterop^.rs)
  2771. else
  2772. taicpu(p).loadconst(2, taicpu(p).oper[2]^.shifterop^.shiftimm);
  2773. result:=true;
  2774. end
  2775. end;
  2776. end;
  2777. begin
  2778. casmoptimizer:=TCpuAsmOptimizer;
  2779. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  2780. End.