aoptcpu.pas 138 KB

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