aoptcpu.pas 123 KB

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