aoptcpu.pas 121 KB

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