aoptcpu.pas 123 KB

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