aoptcpu.pas 123 KB

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