aoptcpu.pas 101 KB

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