aoptcpu.pas 96 KB

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