aoptcpu.pas 118 KB

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