aoptcpu.pas 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  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 GetNextInstructionUsingReg(hp1,hp2, taicpu(p).oper[0]^.reg) and
  707. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp2.Next))) or
  708. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp2)) and
  709. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  710. (taicpu(hp2).ops=3) and
  711. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  712. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  713. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  714. begin
  715. { mov reg1,reg0, lsl imm1
  716. mov reg1,reg1, lsr/asr imm2
  717. mov reg2,reg1, lsl imm3 ...
  718. to
  719. mov reg1,reg0, lsl imm1
  720. mov reg2,reg1, lsr/asr imm2-imm3
  721. if
  722. imm1>=imm2
  723. }
  724. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  725. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  726. (taicpu(p).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  727. begin
  728. if taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm then
  729. begin
  730. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1a done', p);
  731. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm-taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  732. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  733. asml.remove(hp1);
  734. asml.remove(hp2);
  735. hp1.free;
  736. hp2.free;
  737. if taicpu(hp1).oper[2]^.shifterop^.shiftimm>=32 then
  738. begin
  739. taicpu(p).freeop(1);
  740. taicpu(p).freeop(2);
  741. taicpu(p).loadconst(1,0);
  742. end;
  743. end
  744. else
  745. begin
  746. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1b done', p);
  747. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  748. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  749. asml.remove(hp2);
  750. hp2.free;
  751. end;
  752. result := true;
  753. end
  754. { mov reg1,reg0, lsr/asr imm1
  755. mov reg1,reg1, lsl imm2
  756. mov reg1,reg1, lsr/asr imm3 ...
  757. if imm3>=imm1 and imm2>=imm1
  758. to
  759. mov reg1,reg0, lsl imm2-imm1
  760. mov reg1,reg1, lsr/asr imm3 ...
  761. }
  762. 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
  763. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  764. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  765. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  766. begin
  767. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  768. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  769. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  770. asml.remove(p);
  771. p.free;
  772. p:=hp2;
  773. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  774. begin
  775. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  776. asml.remove(hp1);
  777. hp1.free;
  778. p:=hp2;
  779. end;
  780. result := true;
  781. end;
  782. end;
  783. end;
  784. { Change the common
  785. mov r0, r0, lsr #xxx
  786. and r0, r0, #yyy/bic r0, r0, #xxx
  787. and remove the superfluous and/bic if possible
  788. This could be extended to handle more cases.
  789. }
  790. if (taicpu(p).ops=3) and
  791. (taicpu(p).oper[2]^.typ = top_shifterop) and
  792. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  793. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  794. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  795. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  796. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) then
  797. begin
  798. if (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  799. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  800. (taicpu(hp1).ops=3) and
  801. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  802. (taicpu(hp1).oper[2]^.typ = top_const) and
  803. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  804. For LSR #25 and an AndConst of 255 that whould go like this:
  805. 255 and ((2 shl (32-25))-1)
  806. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  807. LSR #25 and AndConst of 254:
  808. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  809. }
  810. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  811. begin
  812. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  813. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  814. asml.remove(hp1);
  815. hp1.free;
  816. result:=true;
  817. end
  818. else if MatchInstruction(hp1, A_BIC, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  819. (taicpu(hp1).ops=3) and
  820. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  821. (taicpu(hp1).oper[2]^.typ = top_const) and
  822. { Check if the BIC actually would only mask out bits beeing already zero because of the shift }
  823. (taicpu(hp1).oper[2]^.val<>0) and
  824. (BsfDWord(taicpu(hp1).oper[2]^.val)>=32-taicpu(p).oper[2]^.shifterop^.shiftimm) then
  825. begin
  826. DebugMsg('Peephole LsrBic2Lsr done', hp1);
  827. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  828. asml.remove(hp1);
  829. hp1.free;
  830. result:=true;
  831. end;
  832. end;
  833. {
  834. optimize
  835. mov rX, yyyy
  836. ....
  837. }
  838. if (taicpu(p).ops = 2) and
  839. GetNextInstruction(p,hp1) and
  840. (tai(hp1).typ = ait_instruction) then
  841. begin
  842. {
  843. This changes the very common
  844. mov r0, #0
  845. str r0, [...]
  846. mov r0, #0
  847. str r0, [...]
  848. and removes all superfluous mov instructions
  849. }
  850. if (taicpu(p).oper[1]^.typ = top_const) and
  851. (taicpu(hp1).opcode=A_STR) then
  852. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  853. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  854. GetNextInstruction(hp1, hp2) and
  855. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  856. (taicpu(hp2).ops = 2) and
  857. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  858. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  859. begin
  860. DebugMsg('Peephole MovStrMov done', hp2);
  861. GetNextInstruction(hp2,hp1);
  862. asml.remove(hp2);
  863. hp2.free;
  864. if not assigned(hp1) then break;
  865. end
  866. {
  867. This removes the first mov from
  868. mov rX,...
  869. mov rX,...
  870. }
  871. else if taicpu(hp1).opcode=A_MOV then
  872. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  873. (taicpu(hp1).ops = 2) and
  874. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  875. { don't remove the first mov if the second is a mov rX,rX }
  876. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  877. begin
  878. DebugMsg('Peephole MovMov done', p);
  879. asml.remove(p);
  880. p.free;
  881. p:=hp1;
  882. GetNextInstruction(hp1,hp1);
  883. if not assigned(hp1) then
  884. break;
  885. end;
  886. end;
  887. {
  888. change
  889. mov r1, r0
  890. add r1, r1, #1
  891. to
  892. add r1, r0, #1
  893. Todo: Make it work for mov+cmp too
  894. CAUTION! If this one is successful p might not be a mov instruction anymore!
  895. }
  896. if (taicpu(p).ops = 2) and
  897. (taicpu(p).oper[1]^.typ = top_reg) and
  898. (taicpu(p).oppostfix = PF_NONE) and
  899. GetNextInstruction(p, hp1) and
  900. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  901. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  902. [taicpu(p).condition], []) and
  903. {MOV and MVN might only have 2 ops}
  904. (taicpu(hp1).ops >= 2) and
  905. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  906. (taicpu(hp1).oper[1]^.typ = top_reg) and
  907. (
  908. (taicpu(hp1).ops = 2) or
  909. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop])
  910. ) then
  911. begin
  912. { When we get here we still don't know if the registers match}
  913. for I:=1 to 2 do
  914. {
  915. If the first loop was successful p will be replaced with hp1.
  916. The checks will still be ok, because all required information
  917. will also be in hp1 then.
  918. }
  919. if (taicpu(hp1).ops > I) and
  920. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  921. begin
  922. DebugMsg('Peephole RedundantMovProcess done', hp1);
  923. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  924. if p<>hp1 then
  925. begin
  926. asml.remove(p);
  927. p.free;
  928. p:=hp1;
  929. end;
  930. end;
  931. end;
  932. { This folds shifterops into following instructions
  933. mov r0, r1, lsl #8
  934. add r2, r3, r0
  935. to
  936. add r2, r3, r1, lsl #8
  937. CAUTION! If this one is successful p might not be a mov instruction anymore!
  938. }
  939. if (taicpu(p).opcode = A_MOV) and
  940. (taicpu(p).ops = 3) and
  941. (taicpu(p).oper[1]^.typ = top_reg) and
  942. (taicpu(p).oper[2]^.typ = top_shifterop) and
  943. (taicpu(p).oppostfix = PF_NONE) and
  944. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  945. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  946. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  947. A_CMP, A_CMN],
  948. [taicpu(p).condition], [PF_None]) and
  949. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  950. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) and
  951. (taicpu(hp1).ops >= 2) and
  952. {Currently we can't fold into another shifterop}
  953. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  954. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  955. NR_DEFAULTFLAGS for modification}
  956. (
  957. {Everything is fine if we don't use RRX}
  958. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  959. (
  960. {If it is RRX, then check if we're just accessing the next instruction}
  961. GetNextInstruction(p, hp2) and
  962. (hp1 = hp2)
  963. )
  964. ) and
  965. { reg1 might not be modified inbetween }
  966. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  967. { The shifterop can contain a register, might not be modified}
  968. (
  969. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  970. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  971. ) and
  972. (
  973. {Only ONE of the two src operands is allowed to match}
  974. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  975. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  976. ) then
  977. begin
  978. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  979. I2:=0
  980. else
  981. I2:=1;
  982. for I:=I2 to taicpu(hp1).ops-1 do
  983. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  984. begin
  985. { If the parameter matched on the second op from the RIGHT
  986. we have to switch the parameters, this will not happen for CMP
  987. were we're only evaluating the most right parameter
  988. }
  989. if I <> taicpu(hp1).ops-1 then
  990. begin
  991. {The SUB operators need to be changed when we swap parameters}
  992. case taicpu(hp1).opcode of
  993. A_SUB: tempop:=A_RSB;
  994. A_SBC: tempop:=A_RSC;
  995. A_RSB: tempop:=A_SUB;
  996. A_RSC: tempop:=A_SBC;
  997. else tempop:=taicpu(hp1).opcode;
  998. end;
  999. if taicpu(hp1).ops = 3 then
  1000. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1001. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1002. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1003. else
  1004. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1005. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1006. taicpu(p).oper[2]^.shifterop^);
  1007. end
  1008. else
  1009. if taicpu(hp1).ops = 3 then
  1010. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1011. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1012. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1013. else
  1014. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1015. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1016. taicpu(p).oper[2]^.shifterop^);
  1017. asml.insertbefore(hp2, hp1);
  1018. asml.remove(p);
  1019. asml.remove(hp1);
  1020. p.free;
  1021. hp1.free;
  1022. p:=hp2;
  1023. GetNextInstruction(p,hp1);
  1024. DebugMsg('Peephole FoldShiftProcess done', p);
  1025. break;
  1026. end;
  1027. end;
  1028. {
  1029. Fold
  1030. mov r1, r1, lsl #2
  1031. ldr/ldrb r0, [r0, r1]
  1032. to
  1033. ldr/ldrb r0, [r0, r1, lsl #2]
  1034. XXX: This still needs some work, as we quite often encounter something like
  1035. mov r1, r2, lsl #2
  1036. add r2, r3, #imm
  1037. ldr r0, [r2, r1]
  1038. which can't be folded because r2 is overwritten between the shift and the ldr.
  1039. We could try to shuffle the registers around and fold it into.
  1040. add r1, r3, #imm
  1041. ldr r0, [r1, r2, lsl #2]
  1042. }
  1043. if (taicpu(p).opcode = A_MOV) and
  1044. (taicpu(p).ops = 3) and
  1045. (taicpu(p).oper[1]^.typ = top_reg) and
  1046. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1047. { RRX is tough to handle, because it requires tracking the C-Flag,
  1048. it is also extremly unlikely to be emitted this way}
  1049. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1050. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1051. (taicpu(p).oppostfix = PF_NONE) and
  1052. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1053. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1054. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition],
  1055. [PF_None, PF_B]) and
  1056. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1057. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg) and
  1058. { Only fold if there isn't another shifterop already. }
  1059. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1060. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1061. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1062. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) then
  1063. begin
  1064. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1065. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1066. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1067. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1068. asml.remove(p);
  1069. p.free;
  1070. p:=hp1;
  1071. end;
  1072. {
  1073. Often we see shifts and then a superfluous mov to another register
  1074. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1075. }
  1076. if (taicpu(p).opcode = A_MOV) and
  1077. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1078. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  1079. end;
  1080. A_ADD,
  1081. A_ADC,
  1082. A_RSB,
  1083. A_RSC,
  1084. A_SUB,
  1085. A_SBC,
  1086. A_AND,
  1087. A_BIC,
  1088. A_EOR,
  1089. A_ORR,
  1090. A_MLA,
  1091. A_MUL:
  1092. begin
  1093. {
  1094. optimize
  1095. and reg2,reg1,const1
  1096. ...
  1097. }
  1098. if (taicpu(p).opcode = A_AND) and
  1099. (taicpu(p).ops>2) and
  1100. (taicpu(p).oper[1]^.typ = top_reg) and
  1101. (taicpu(p).oper[2]^.typ = top_const) then
  1102. begin
  1103. {
  1104. change
  1105. and reg2,reg1,const1
  1106. and reg3,reg2,const2
  1107. to
  1108. and reg3,reg1,(const1 and const2)
  1109. }
  1110. if GetNextInstruction(p, hp1) and
  1111. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1112. { either reg3 and reg2 are equal or reg2 is deallocated after the and }
  1113. (MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) or
  1114. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next)))) and
  1115. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1116. (taicpu(hp1).oper[2]^.typ = top_const) then
  1117. begin
  1118. DebugMsg('Peephole AndAnd2And done', p);
  1119. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1120. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1121. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1122. asml.remove(hp1);
  1123. hp1.free;
  1124. end
  1125. {
  1126. change
  1127. and reg2,reg1,255
  1128. strb reg2,[...]
  1129. dealloc reg2
  1130. to
  1131. strb reg1,[...]
  1132. }
  1133. else if (taicpu(p).oper[2]^.val = 255) and
  1134. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1135. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1136. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1137. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1138. { the reference in strb might not use reg2 }
  1139. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1140. { reg1 might not be modified inbetween }
  1141. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1142. begin
  1143. DebugMsg('Peephole AndStrb2Strb done', p);
  1144. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1145. asml.remove(p);
  1146. p.free;
  1147. p:=hp1;
  1148. end
  1149. {
  1150. from
  1151. and reg1,reg0,2^n-1
  1152. mov reg2,reg1, lsl imm1
  1153. (mov reg3,reg2, lsr/asr imm1)
  1154. remove either the and or the lsl/xsr sequence if possible
  1155. }
  1156. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1157. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1158. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1159. (taicpu(hp1).ops=3) and
  1160. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1161. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1162. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1163. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1164. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1165. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) then
  1166. begin
  1167. {
  1168. and reg1,reg0,2^n-1
  1169. mov reg2,reg1, lsl imm1
  1170. mov reg3,reg2, lsr/asr imm1
  1171. =>
  1172. and reg1,reg0,2^n-1
  1173. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1174. }
  1175. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1176. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1177. (taicpu(hp2).ops=3) and
  1178. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1179. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1180. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1181. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1182. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1183. (assigned(FindRegDealloc(taicpu(hp1).oper[0]^.reg,tai(hp2.Next))) or
  1184. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp2)) and
  1185. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1186. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1187. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1188. begin
  1189. DebugMsg('Peephole AndLslXsr2And done', p);
  1190. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1191. asml.Remove(hp1);
  1192. asml.Remove(hp2);
  1193. hp1.free;
  1194. hp2.free;
  1195. result:=true;
  1196. end
  1197. {
  1198. and reg1,reg0,2^n-1
  1199. mov reg2,reg1, lsl imm1
  1200. =>
  1201. mov reg2,reg1, lsl imm1
  1202. if imm1>i
  1203. }
  1204. else if i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm then
  1205. begin
  1206. DebugMsg('Peephole AndLsl2Lsl done', p);
  1207. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[0]^.reg;
  1208. asml.Remove(p);
  1209. p.free;
  1210. p:=hp1;
  1211. result:=true;
  1212. end
  1213. end;
  1214. end;
  1215. {
  1216. change
  1217. add/sub reg2,reg1,const1
  1218. str/ldr reg3,[reg2,const2]
  1219. dealloc reg2
  1220. to
  1221. str/ldr reg3,[reg1,const2+/-const1]
  1222. }
  1223. if (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1224. (taicpu(p).ops>2) and
  1225. (taicpu(p).oper[1]^.typ = top_reg) and
  1226. (taicpu(p).oper[2]^.typ = top_const) then
  1227. begin
  1228. hp1:=p;
  1229. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1230. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1231. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1232. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1233. { don't optimize if the register is stored/overwritten }
  1234. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1235. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1236. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1237. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1238. ldr postfix }
  1239. (((taicpu(p).opcode=A_ADD) and
  1240. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1241. ) or
  1242. ((taicpu(p).opcode=A_SUB) and
  1243. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1244. )
  1245. ) do
  1246. begin
  1247. { neither reg1 nor reg2 might be changed inbetween }
  1248. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1249. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1250. break;
  1251. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1252. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1253. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1254. begin
  1255. { remember last instruction }
  1256. hp2:=hp1;
  1257. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1258. hp1:=p;
  1259. { fix all ldr/str }
  1260. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1261. begin
  1262. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1263. if taicpu(p).opcode=A_ADD then
  1264. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1265. else
  1266. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1267. if hp1=hp2 then
  1268. break;
  1269. end;
  1270. GetNextInstruction(p,hp1);
  1271. asml.remove(p);
  1272. p.free;
  1273. p:=hp1;
  1274. break;
  1275. end;
  1276. end;
  1277. end;
  1278. {
  1279. change
  1280. add reg1, ...
  1281. mov reg2, reg1
  1282. to
  1283. add reg2, ...
  1284. }
  1285. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1286. begin
  1287. if (taicpu(p).ops=3) then
  1288. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  1289. end;
  1290. end;
  1291. {$ifdef dummy}
  1292. A_MVN:
  1293. begin
  1294. {
  1295. change
  1296. mvn reg2,reg1
  1297. and reg3,reg4,reg2
  1298. dealloc reg2
  1299. to
  1300. bic reg3,reg4,reg1
  1301. }
  1302. if (taicpu(p).oper[1]^.typ = top_reg) and
  1303. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1304. MatchInstruction(hp1,A_AND,[],[]) and
  1305. (((taicpu(hp1).ops=3) and
  1306. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1307. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1308. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1309. ((taicpu(hp1).ops=2) and
  1310. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1311. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1312. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1313. { reg1 might not be modified inbetween }
  1314. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1315. begin
  1316. DebugMsg('Peephole MvnAnd2Bic done', p);
  1317. taicpu(hp1).opcode:=A_BIC;
  1318. if taicpu(hp1).ops=3 then
  1319. begin
  1320. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1321. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1322. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1323. end
  1324. else
  1325. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1326. asml.remove(p);
  1327. p.free;
  1328. p:=hp1;
  1329. end;
  1330. end;
  1331. {$endif dummy}
  1332. A_UXTB:
  1333. begin
  1334. {
  1335. change
  1336. uxtb reg2,reg1
  1337. strb reg2,[...]
  1338. dealloc reg2
  1339. to
  1340. strb reg1,[...]
  1341. }
  1342. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1343. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1344. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1345. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1346. { the reference in strb might not use reg2 }
  1347. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1348. { reg1 might not be modified inbetween }
  1349. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1350. begin
  1351. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1352. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1353. asml.remove(p);
  1354. p.free;
  1355. p:=hp1;
  1356. end
  1357. {
  1358. change
  1359. uxtb reg2,reg1
  1360. uxth reg3,reg2
  1361. dealloc reg2
  1362. to
  1363. uxtb reg3,reg1
  1364. }
  1365. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1366. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1367. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1368. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1369. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1370. { reg1 might not be modified inbetween }
  1371. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1372. begin
  1373. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1374. taicpu(hp1).opcode:=A_UXTB;
  1375. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1376. asml.remove(p);
  1377. p.free;
  1378. p:=hp1;
  1379. end
  1380. {
  1381. change
  1382. uxtb reg2,reg1
  1383. uxtb reg3,reg2
  1384. dealloc reg2
  1385. to
  1386. uxtb reg3,reg1
  1387. }
  1388. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1389. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1390. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1391. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1392. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1393. { reg1 might not be modified inbetween }
  1394. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1395. begin
  1396. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1397. taicpu(hp1).opcode:=A_UXTB;
  1398. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1399. asml.remove(p);
  1400. p.free;
  1401. p:=hp1;
  1402. end
  1403. {
  1404. change
  1405. uxth reg2,reg1
  1406. uxth reg3,reg2
  1407. dealloc reg2
  1408. to
  1409. uxth reg3,reg1
  1410. }
  1411. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1412. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1413. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1414. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1415. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1416. { reg1 might not be modified inbetween }
  1417. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1418. begin
  1419. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1420. taicpu(hp1).opcode:=A_UXTH;
  1421. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1422. asml.remove(p);
  1423. p.free;
  1424. p:=hp1;
  1425. end;
  1426. end;
  1427. A_UXTH:
  1428. begin
  1429. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1430. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1431. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1432. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1433. { the reference in strb might not use reg2 }
  1434. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1435. { reg1 might not be modified inbetween }
  1436. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1437. begin
  1438. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1439. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1440. asml.remove(p);
  1441. p.free;
  1442. p:=hp1;
  1443. end;
  1444. end;
  1445. A_CMP:
  1446. begin
  1447. {
  1448. change
  1449. cmp reg,const1
  1450. moveq reg,const1
  1451. movne reg,const2
  1452. to
  1453. cmp reg,const1
  1454. movne reg,const2
  1455. }
  1456. if (taicpu(p).oper[1]^.typ = top_const) and
  1457. GetNextInstruction(p, hp1) and
  1458. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1459. (taicpu(hp1).oper[1]^.typ = top_const) and
  1460. GetNextInstruction(hp1, hp2) and
  1461. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1462. (taicpu(hp1).oper[1]^.typ = top_const) then
  1463. begin
  1464. RemoveRedundantMove(p, hp1, asml);
  1465. RemoveRedundantMove(p, hp2, asml);
  1466. end;
  1467. end;
  1468. A_STM:
  1469. begin
  1470. {
  1471. change
  1472. stmfd r13!,[r14]
  1473. sub r13,r13,#4
  1474. bl abc
  1475. add r13,r13,#4
  1476. ldmfd r13!,[r15]
  1477. into
  1478. b abc
  1479. }
  1480. if MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  1481. GetNextInstruction(p, hp1) and
  1482. GetNextInstruction(hp1, hp2) and
  1483. SkipEntryExitMarker(hp2, hp2) and
  1484. GetNextInstruction(hp2, hp3) and
  1485. SkipEntryExitMarker(hp3, hp3) and
  1486. GetNextInstruction(hp3, hp4) and
  1487. (taicpu(p).oper[0]^.typ = top_ref) and
  1488. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1489. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  1490. (taicpu(p).oper[0]^.ref^.offset=0) and
  1491. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1492. (taicpu(p).oper[1]^.typ = top_regset) and
  1493. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  1494. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  1495. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1496. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1497. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1498. (taicpu(hp1).oper[1]^.reg = NR_STACK_POINTER_REG) and
  1499. (taicpu(hp1).oper[2]^.typ = top_const) and
  1500. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  1501. (taicpu(hp3).oper[0]^.typ = top_reg) and
  1502. (taicpu(hp3).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1503. (taicpu(hp3).oper[1]^.typ = top_reg) and
  1504. (taicpu(hp3).oper[1]^.reg = NR_STACK_POINTER_REG) and
  1505. (taicpu(hp3).oper[2]^.typ = top_const) and
  1506. (taicpu(hp1).oper[2]^.val = taicpu(hp3).oper[2]^.val) and
  1507. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  1508. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1509. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  1510. (taicpu(hp4).oper[0]^.typ = top_ref) and
  1511. (taicpu(hp4).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1512. (taicpu(hp4).oper[0]^.ref^.base=NR_NO) and
  1513. (taicpu(hp4).oper[0]^.ref^.offset=0) and
  1514. (taicpu(hp4).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1515. (taicpu(hp4).oper[1]^.typ = top_regset) and
  1516. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  1517. begin
  1518. asml.Remove(p);
  1519. asml.Remove(hp1);
  1520. asml.Remove(hp3);
  1521. asml.Remove(hp4);
  1522. taicpu(hp2).opcode:=A_B;
  1523. p.free;
  1524. hp1.free;
  1525. hp3.free;
  1526. hp4.free;
  1527. p:=hp2;
  1528. DebugMsg('Peephole Bl2B done', p);
  1529. end;
  1530. end;
  1531. end;
  1532. end;
  1533. end;
  1534. end;
  1535. { instructions modifying the CPSR can be only the last instruction }
  1536. function MustBeLast(p : tai) : boolean;
  1537. begin
  1538. Result:=(p.typ=ait_instruction) and
  1539. ((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
  1540. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1541. (taicpu(p).oppostfix=PF_S));
  1542. end;
  1543. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1544. var
  1545. p,hp1,hp2: tai;
  1546. l : longint;
  1547. condition : tasmcond;
  1548. hp3: tai;
  1549. WasLast: boolean;
  1550. { UsedRegs, TmpUsedRegs: TRegSet; }
  1551. begin
  1552. p := BlockStart;
  1553. { UsedRegs := []; }
  1554. while (p <> BlockEnd) Do
  1555. begin
  1556. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1557. case p.Typ Of
  1558. Ait_Instruction:
  1559. begin
  1560. case taicpu(p).opcode Of
  1561. A_B:
  1562. if taicpu(p).condition<>C_None then
  1563. begin
  1564. { check for
  1565. Bxx xxx
  1566. <several instructions>
  1567. xxx:
  1568. }
  1569. l:=0;
  1570. WasLast:=False;
  1571. GetNextInstruction(p, hp1);
  1572. while assigned(hp1) and
  1573. (l<=4) and
  1574. CanBeCond(hp1) and
  1575. { stop on labels }
  1576. not(hp1.typ=ait_label) do
  1577. begin
  1578. inc(l);
  1579. if MustBeLast(hp1) then
  1580. begin
  1581. WasLast:=True;
  1582. GetNextInstruction(hp1,hp1);
  1583. break;
  1584. end
  1585. else
  1586. GetNextInstruction(hp1,hp1);
  1587. end;
  1588. if assigned(hp1) then
  1589. begin
  1590. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1591. begin
  1592. if (l<=4) and (l>0) then
  1593. begin
  1594. condition:=inverse_cond(taicpu(p).condition);
  1595. hp2:=p;
  1596. GetNextInstruction(p,hp1);
  1597. p:=hp1;
  1598. repeat
  1599. if hp1.typ=ait_instruction then
  1600. taicpu(hp1).condition:=condition;
  1601. if MustBeLast(hp1) then
  1602. begin
  1603. GetNextInstruction(hp1,hp1);
  1604. break;
  1605. end
  1606. else
  1607. GetNextInstruction(hp1,hp1);
  1608. until not(assigned(hp1)) or
  1609. not(CanBeCond(hp1)) or
  1610. (hp1.typ=ait_label);
  1611. { wait with removing else GetNextInstruction could
  1612. ignore the label if it was the only usage in the
  1613. jump moved away }
  1614. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1615. asml.remove(hp2);
  1616. hp2.free;
  1617. continue;
  1618. end;
  1619. end
  1620. else
  1621. { do not perform further optimizations if there is inctructon
  1622. in block #1 which can not be optimized.
  1623. }
  1624. if not WasLast then
  1625. begin
  1626. { check further for
  1627. Bcc xxx
  1628. <several instructions 1>
  1629. B yyy
  1630. xxx:
  1631. <several instructions 2>
  1632. yyy:
  1633. }
  1634. { hp2 points to jmp yyy }
  1635. hp2:=hp1;
  1636. { skip hp1 to xxx }
  1637. GetNextInstruction(hp1, hp1);
  1638. if assigned(hp2) and
  1639. assigned(hp1) and
  1640. (l<=3) and
  1641. (hp2.typ=ait_instruction) and
  1642. (taicpu(hp2).is_jmp) and
  1643. (taicpu(hp2).condition=C_None) and
  1644. { real label and jump, no further references to the
  1645. label are allowed }
  1646. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1647. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1648. begin
  1649. l:=0;
  1650. { skip hp1 to <several moves 2> }
  1651. GetNextInstruction(hp1, hp1);
  1652. while assigned(hp1) and
  1653. CanBeCond(hp1) do
  1654. begin
  1655. inc(l);
  1656. GetNextInstruction(hp1, hp1);
  1657. end;
  1658. { hp1 points to yyy: }
  1659. if assigned(hp1) and
  1660. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1661. begin
  1662. condition:=inverse_cond(taicpu(p).condition);
  1663. GetNextInstruction(p,hp1);
  1664. hp3:=p;
  1665. p:=hp1;
  1666. repeat
  1667. if hp1.typ=ait_instruction then
  1668. taicpu(hp1).condition:=condition;
  1669. GetNextInstruction(hp1,hp1);
  1670. until not(assigned(hp1)) or
  1671. not(CanBeCond(hp1));
  1672. { hp2 is still at jmp yyy }
  1673. GetNextInstruction(hp2,hp1);
  1674. { hp2 is now at xxx: }
  1675. condition:=inverse_cond(condition);
  1676. GetNextInstruction(hp1,hp1);
  1677. { hp1 is now at <several movs 2> }
  1678. repeat
  1679. taicpu(hp1).condition:=condition;
  1680. GetNextInstruction(hp1,hp1);
  1681. until not(assigned(hp1)) or
  1682. not(CanBeCond(hp1)) or
  1683. (hp1.typ=ait_label);
  1684. {
  1685. asml.remove(hp1.next)
  1686. hp1.next.free;
  1687. asml.remove(hp1);
  1688. hp1.free;
  1689. }
  1690. { remove Bcc }
  1691. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1692. asml.remove(hp3);
  1693. hp3.free;
  1694. { remove jmp }
  1695. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1696. asml.remove(hp2);
  1697. hp2.free;
  1698. continue;
  1699. end;
  1700. end;
  1701. end;
  1702. end;
  1703. end;
  1704. end;
  1705. end;
  1706. end;
  1707. p := tai(p.next)
  1708. end;
  1709. end;
  1710. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1711. begin
  1712. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1713. Result:=true
  1714. else
  1715. Result:=inherited RegInInstruction(Reg, p1);
  1716. end;
  1717. const
  1718. { set of opcode which might or do write to memory }
  1719. { TODO : extend armins.dat to contain r/w info }
  1720. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1721. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1722. { adjust the register live information when swapping the two instructions p and hp1,
  1723. they must follow one after the other }
  1724. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  1725. procedure CheckLiveEnd(reg : tregister);
  1726. var
  1727. supreg : TSuperRegister;
  1728. regtype : TRegisterType;
  1729. begin
  1730. if reg=NR_NO then
  1731. exit;
  1732. regtype:=getregtype(reg);
  1733. supreg:=getsupreg(reg);
  1734. if (cg.rg[regtype].live_end[supreg]=hp1) and
  1735. RegInInstruction(reg,p) then
  1736. cg.rg[regtype].live_end[supreg]:=p;
  1737. end;
  1738. procedure CheckLiveStart(reg : TRegister);
  1739. var
  1740. supreg : TSuperRegister;
  1741. regtype : TRegisterType;
  1742. begin
  1743. if reg=NR_NO then
  1744. exit;
  1745. regtype:=getregtype(reg);
  1746. supreg:=getsupreg(reg);
  1747. if (cg.rg[regtype].live_start[supreg]=p) and
  1748. RegInInstruction(reg,hp1) then
  1749. cg.rg[regtype].live_start[supreg]:=hp1;
  1750. end;
  1751. var
  1752. i : longint;
  1753. r : TSuperRegister;
  1754. begin
  1755. { assumption: p is directly followed by hp1 }
  1756. { if live of any reg used by p starts at p and hp1 uses this register then
  1757. set live start to hp1 }
  1758. for i:=0 to p.ops-1 do
  1759. case p.oper[i]^.typ of
  1760. Top_Reg:
  1761. CheckLiveStart(p.oper[i]^.reg);
  1762. Top_Ref:
  1763. begin
  1764. CheckLiveStart(p.oper[i]^.ref^.base);
  1765. CheckLiveStart(p.oper[i]^.ref^.index);
  1766. end;
  1767. Top_Shifterop:
  1768. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  1769. Top_RegSet:
  1770. for r:=RS_R0 to RS_R15 do
  1771. if r in p.oper[i]^.regset^ then
  1772. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1773. end;
  1774. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  1775. set live end to p }
  1776. for i:=0 to hp1.ops-1 do
  1777. case hp1.oper[i]^.typ of
  1778. Top_Reg:
  1779. CheckLiveEnd(hp1.oper[i]^.reg);
  1780. Top_Ref:
  1781. begin
  1782. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  1783. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  1784. end;
  1785. Top_Shifterop:
  1786. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  1787. Top_RegSet:
  1788. for r:=RS_R0 to RS_R15 do
  1789. if r in hp1.oper[i]^.regset^ then
  1790. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1791. end;
  1792. end;
  1793. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  1794. { TODO : schedule also forward }
  1795. { TODO : schedule distance > 1 }
  1796. var
  1797. hp1,hp2,hp3,hp4,hp5 : tai;
  1798. list : TAsmList;
  1799. begin
  1800. result:=true;
  1801. list:=TAsmList.Create;
  1802. p:=BlockStart;
  1803. while p<>BlockEnd Do
  1804. begin
  1805. if (p.typ=ait_instruction) and
  1806. GetNextInstruction(p,hp1) and
  1807. (hp1.typ=ait_instruction) and
  1808. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1809. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1810. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1811. not(RegModifiedByInstruction(NR_PC,p))
  1812. ) or
  1813. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1814. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1815. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1816. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1817. )
  1818. ) or
  1819. { try to prove that the memory accesses don't overlapp }
  1820. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1821. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1822. (taicpu(p).oppostfix=PF_None) and
  1823. (taicpu(hp1).oppostfix=PF_None) and
  1824. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1825. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1826. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1827. (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)]))
  1828. )
  1829. )
  1830. ) and
  1831. GetNextInstruction(hp1,hp2) and
  1832. (hp2.typ=ait_instruction) and
  1833. { loaded register used by next instruction? }
  1834. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1835. { loaded register not used by previous instruction? }
  1836. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1837. { same condition? }
  1838. (taicpu(p).condition=taicpu(hp1).condition) and
  1839. { first instruction might not change the register used as base }
  1840. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1841. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1842. ) and
  1843. { first instruction might not change the register used as index }
  1844. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1845. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1846. ) then
  1847. begin
  1848. hp3:=tai(p.Previous);
  1849. hp5:=tai(p.next);
  1850. asml.Remove(p);
  1851. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1852. { before the instruction? }
  1853. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1854. begin
  1855. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1856. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1857. begin
  1858. hp4:=hp3;
  1859. hp3:=tai(hp3.Previous);
  1860. asml.Remove(hp4);
  1861. list.Concat(hp4);
  1862. end
  1863. else
  1864. hp3:=tai(hp3.Previous);
  1865. end;
  1866. list.Concat(p);
  1867. SwapRegLive(taicpu(p),taicpu(hp1));
  1868. { after the instruction? }
  1869. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1870. begin
  1871. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1872. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1873. begin
  1874. hp4:=hp5;
  1875. hp5:=tai(hp5.next);
  1876. asml.Remove(hp4);
  1877. list.Concat(hp4);
  1878. end
  1879. else
  1880. hp5:=tai(hp5.Next);
  1881. end;
  1882. asml.Remove(hp1);
  1883. {$ifdef DEBUG_PREREGSCHEDULER}
  1884. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1885. {$endif DEBUG_PREREGSCHEDULER}
  1886. asml.InsertBefore(hp1,hp2);
  1887. asml.InsertListBefore(hp2,list);
  1888. p:=tai(p.next)
  1889. end
  1890. else if p.typ=ait_instruction then
  1891. p:=hp1
  1892. else
  1893. p:=tai(p.next);
  1894. end;
  1895. list.Free;
  1896. end;
  1897. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  1898. var
  1899. hp : tai;
  1900. l : longint;
  1901. begin
  1902. hp := tai(p.Previous);
  1903. l := 1;
  1904. while assigned(hp) and
  1905. (l <= 4) do
  1906. begin
  1907. if hp.typ=ait_instruction then
  1908. begin
  1909. if (taicpu(hp).opcode>=A_IT) and
  1910. (taicpu(hp).opcode <= A_ITTTT) then
  1911. begin
  1912. if (taicpu(hp).opcode = A_IT) and
  1913. (l=1) then
  1914. list.Remove(hp)
  1915. else
  1916. case taicpu(hp).opcode of
  1917. A_ITE:
  1918. if l=2 then taicpu(hp).opcode := A_IT;
  1919. A_ITT:
  1920. if l=2 then taicpu(hp).opcode := A_IT;
  1921. A_ITEE:
  1922. if l=3 then taicpu(hp).opcode := A_ITE;
  1923. A_ITTE:
  1924. if l=3 then taicpu(hp).opcode := A_ITT;
  1925. A_ITET:
  1926. if l=3 then taicpu(hp).opcode := A_ITE;
  1927. A_ITTT:
  1928. if l=3 then taicpu(hp).opcode := A_ITT;
  1929. A_ITEEE:
  1930. if l=4 then taicpu(hp).opcode := A_ITEE;
  1931. A_ITTEE:
  1932. if l=4 then taicpu(hp).opcode := A_ITTE;
  1933. A_ITETE:
  1934. if l=4 then taicpu(hp).opcode := A_ITET;
  1935. A_ITTTE:
  1936. if l=4 then taicpu(hp).opcode := A_ITTT;
  1937. A_ITEET:
  1938. if l=4 then taicpu(hp).opcode := A_ITEE;
  1939. A_ITTET:
  1940. if l=4 then taicpu(hp).opcode := A_ITTE;
  1941. A_ITETT:
  1942. if l=4 then taicpu(hp).opcode := A_ITET;
  1943. A_ITTTT:
  1944. if l=4 then taicpu(hp).opcode := A_ITTT;
  1945. end;
  1946. break;
  1947. end;
  1948. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  1949. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  1950. break;}
  1951. inc(l);
  1952. end;
  1953. hp := tai(hp.Previous);
  1954. end;
  1955. end;
  1956. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1957. var
  1958. hp : taicpu;
  1959. hp1,hp2 : tai;
  1960. begin
  1961. if (p.typ=ait_instruction) and
  1962. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  1963. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1964. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1965. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  1966. begin
  1967. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1968. AsmL.InsertAfter(hp, p);
  1969. asml.Remove(p);
  1970. p:=hp;
  1971. result:=true;
  1972. end
  1973. else if (p.typ=ait_instruction) and
  1974. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  1975. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  1976. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  1977. (taicpu(p).oper[1]^.ref^.offset=-4) and
  1978. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  1979. begin
  1980. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  1981. asml.InsertAfter(hp, p);
  1982. asml.Remove(p);
  1983. p.Free;
  1984. p:=hp;
  1985. result:=true;
  1986. end
  1987. else if (p.typ=ait_instruction) and
  1988. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  1989. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1990. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1991. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  1992. begin
  1993. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1994. asml.InsertBefore(hp, p);
  1995. asml.Remove(p);
  1996. p.Free;
  1997. p:=hp;
  1998. result:=true;
  1999. end
  2000. else if (p.typ=ait_instruction) and
  2001. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  2002. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  2003. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2004. (taicpu(p).oper[1]^.ref^.offset=4) and
  2005. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  2006. begin
  2007. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2008. asml.InsertBefore(hp, p);
  2009. asml.Remove(p);
  2010. p.Free;
  2011. p:=hp;
  2012. result:=true;
  2013. end
  2014. else if (p.typ=ait_instruction) and
  2015. MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  2016. (taicpu(p).oper[1]^.typ=top_const) and
  2017. (taicpu(p).oper[1]^.val >= 0) and
  2018. (taicpu(p).oper[1]^.val < 256) and
  2019. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2020. begin
  2021. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2022. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2023. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2024. taicpu(p).oppostfix:=PF_S;
  2025. result:=true;
  2026. end
  2027. else if (p.typ=ait_instruction) and
  2028. MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  2029. (taicpu(p).oper[1]^.typ=top_reg) and
  2030. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2031. begin
  2032. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2033. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2034. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2035. taicpu(p).oppostfix:=PF_S;
  2036. result:=true;
  2037. end
  2038. else if (p.typ=ait_instruction) and
  2039. MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2040. (taicpu(p).ops = 3) and
  2041. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2042. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2043. (taicpu(p).oper[2]^.typ=top_const) and
  2044. (taicpu(p).oper[2]^.val >= 0) and
  2045. (taicpu(p).oper[2]^.val < 256) and
  2046. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2047. begin
  2048. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2049. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2050. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2051. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2052. taicpu(p).oppostfix:=PF_S;
  2053. taicpu(p).ops := 2;
  2054. result:=true;
  2055. end
  2056. else if (p.typ=ait_instruction) and
  2057. MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  2058. (taicpu(p).ops = 3) and
  2059. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2060. (taicpu(p).oper[2]^.typ=top_reg) then
  2061. begin
  2062. taicpu(p).ops := 2;
  2063. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2064. result:=true;
  2065. end
  2066. else if (p.typ=ait_instruction) and
  2067. MatchInstruction(p, [A_AND,A_ORR,A_EOR,A_BIC,A_LSL,A_LSR,A_ASR,A_ROR], [C_None], [PF_None]) and
  2068. (taicpu(p).ops = 3) and
  2069. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2070. (taicpu(p).oper[2]^.typ=top_reg) and
  2071. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2072. begin
  2073. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2074. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2075. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2076. taicpu(p).ops := 2;
  2077. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2078. taicpu(p).oppostfix:=PF_S;
  2079. result:=true;
  2080. end
  2081. else if (p.typ=ait_instruction) and
  2082. MatchInstruction(p, [A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  2083. (taicpu(p).ops = 3) and
  2084. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  2085. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2086. begin
  2087. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2088. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2089. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2090. taicpu(p).oppostfix:=PF_S;
  2091. taicpu(p).ops := 2;
  2092. result:=true;
  2093. end
  2094. else if (p.typ=ait_instruction) and
  2095. MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  2096. (taicpu(p).ops=3) and
  2097. (taicpu(p).oper[2]^.typ=top_shifterop) and
  2098. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  2099. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2100. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2101. begin
  2102. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2103. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2104. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2105. taicpu(p).oppostfix:=PF_S;
  2106. taicpu(p).ops := 2;
  2107. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  2108. taicpu(p).loadreg(1, taicpu(p).oper[2]^.shifterop^.rs)
  2109. else
  2110. taicpu(p).loadconst(1, taicpu(p).oper[2]^.shifterop^.shiftimm);
  2111. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  2112. SM_LSL: taicpu(p).opcode:=A_LSL;
  2113. SM_LSR: taicpu(p).opcode:=A_LSR;
  2114. SM_ASR: taicpu(p).opcode:=A_ASR;
  2115. SM_ROR: taicpu(p).opcode:=A_ROR;
  2116. end;
  2117. result:=true;
  2118. end
  2119. else if (p.typ=ait_instruction) and
  2120. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2121. (taicpu(p).ops = 2) and
  2122. (taicpu(p).oper[1]^.typ=top_const) and
  2123. ((taicpu(p).oper[1]^.val=255) or
  2124. (taicpu(p).oper[1]^.val=65535)) then
  2125. begin
  2126. if taicpu(p).oper[1]^.val=255 then
  2127. taicpu(p).opcode:=A_UXTB
  2128. else
  2129. taicpu(p).opcode:=A_UXTH;
  2130. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  2131. result := true;
  2132. end
  2133. else if (p.typ=ait_instruction) and
  2134. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2135. (taicpu(p).ops = 3) and
  2136. (taicpu(p).oper[2]^.typ=top_const) and
  2137. ((taicpu(p).oper[2]^.val=255) or
  2138. (taicpu(p).oper[2]^.val=65535)) then
  2139. begin
  2140. if taicpu(p).oper[2]^.val=255 then
  2141. taicpu(p).opcode:=A_UXTB
  2142. else
  2143. taicpu(p).opcode:=A_UXTH;
  2144. taicpu(p).ops:=2;
  2145. result := true;
  2146. end
  2147. {
  2148. Turn
  2149. mul reg0, z,w
  2150. sub/add x, y, reg0
  2151. dealloc reg0
  2152. into
  2153. mls/mla x,y,z,w
  2154. }
  2155. {
  2156. According to Jeppe Johansen this currently uses operands in the wrong order.
  2157. else if (p.typ=ait_instruction) and
  2158. MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  2159. (taicpu(p).ops=3) and
  2160. (taicpu(p).oper[0]^.typ = top_reg) and
  2161. (taicpu(p).oper[1]^.typ = top_reg) and
  2162. (taicpu(p).oper[2]^.typ = top_reg) and
  2163. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  2164. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  2165. (((taicpu(hp1).ops=3) and
  2166. (taicpu(hp1).oper[2]^.typ=top_reg) and
  2167. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  2168. (MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  2169. (taicpu(hp1).opcode=A_ADD)))) or
  2170. ((taicpu(hp1).ops=2) and
  2171. (taicpu(hp1).oper[1]^.typ=top_reg) and
  2172. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  2173. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  2174. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  2175. not(RegModifiedBetween(taicpu(p).oper[2]^.reg,p,hp1)) then
  2176. begin
  2177. if taicpu(hp1).opcode=A_ADD then
  2178. begin
  2179. taicpu(hp1).opcode:=A_MLA;
  2180. if taicpu(hp1).ops=3 then
  2181. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  2182. taicpu(hp1).loadreg(1,taicpu(hp1).oper[2]^.reg);
  2183. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  2184. taicpu(hp1).loadreg(3,taicpu(p).oper[2]^.reg);
  2185. DebugMsg('MulAdd2MLA done', p);
  2186. taicpu(hp1).ops:=4;
  2187. asml.remove(p);
  2188. p.free;
  2189. p:=hp1;
  2190. end
  2191. else
  2192. begin
  2193. taicpu(hp1).opcode:=A_MLS;
  2194. if taicpu(hp1).ops=2 then
  2195. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2196. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  2197. taicpu(hp1).loadreg(3,taicpu(p).oper[2]^.reg);
  2198. DebugMsg('MulSub2MLS done', p);
  2199. taicpu(hp1).ops:=4;
  2200. asml.remove(p);
  2201. p.free;
  2202. p:=hp1;
  2203. end;
  2204. result:=true;
  2205. end
  2206. }
  2207. {else if (p.typ=ait_instruction) and
  2208. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  2209. (taicpu(p).oper[1]^.typ=top_const) and
  2210. (taicpu(p).oper[1]^.val=0) and
  2211. GetNextInstruction(p,hp1) and
  2212. (taicpu(hp1).opcode=A_B) and
  2213. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  2214. begin
  2215. if taicpu(hp1).condition = C_EQ then
  2216. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  2217. else
  2218. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  2219. taicpu(hp2).is_jmp := true;
  2220. asml.InsertAfter(hp2, hp1);
  2221. asml.Remove(hp1);
  2222. hp1.Free;
  2223. asml.Remove(p);
  2224. p.Free;
  2225. p := hp2;
  2226. result := true;
  2227. end}
  2228. else
  2229. Result := inherited PeepHoleOptPass1Cpu(p);
  2230. end;
  2231. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2232. var
  2233. p,hp1,hp2: tai;
  2234. l,l2 : longint;
  2235. condition : tasmcond;
  2236. hp3: tai;
  2237. WasLast: boolean;
  2238. { UsedRegs, TmpUsedRegs: TRegSet; }
  2239. begin
  2240. p := BlockStart;
  2241. { UsedRegs := []; }
  2242. while (p <> BlockEnd) Do
  2243. begin
  2244. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2245. case p.Typ Of
  2246. Ait_Instruction:
  2247. begin
  2248. case taicpu(p).opcode Of
  2249. A_B:
  2250. if taicpu(p).condition<>C_None then
  2251. begin
  2252. { check for
  2253. Bxx xxx
  2254. <several instructions>
  2255. xxx:
  2256. }
  2257. l:=0;
  2258. GetNextInstruction(p, hp1);
  2259. while assigned(hp1) and
  2260. (l<=4) and
  2261. CanBeCond(hp1) and
  2262. { stop on labels }
  2263. not(hp1.typ=ait_label) do
  2264. begin
  2265. inc(l);
  2266. if MustBeLast(hp1) then
  2267. begin
  2268. //hp1:=nil;
  2269. GetNextInstruction(hp1,hp1);
  2270. break;
  2271. end
  2272. else
  2273. GetNextInstruction(hp1,hp1);
  2274. end;
  2275. if assigned(hp1) then
  2276. begin
  2277. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2278. begin
  2279. if (l<=4) and (l>0) then
  2280. begin
  2281. condition:=inverse_cond(taicpu(p).condition);
  2282. hp2:=p;
  2283. GetNextInstruction(p,hp1);
  2284. p:=hp1;
  2285. repeat
  2286. if hp1.typ=ait_instruction then
  2287. taicpu(hp1).condition:=condition;
  2288. if MustBeLast(hp1) then
  2289. begin
  2290. GetNextInstruction(hp1,hp1);
  2291. break;
  2292. end
  2293. else
  2294. GetNextInstruction(hp1,hp1);
  2295. until not(assigned(hp1)) or
  2296. not(CanBeCond(hp1)) or
  2297. (hp1.typ=ait_label);
  2298. { wait with removing else GetNextInstruction could
  2299. ignore the label if it was the only usage in the
  2300. jump moved away }
  2301. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2302. DecrementPreceedingIT(asml, hp2);
  2303. case l of
  2304. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2305. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2306. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2307. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2308. end;
  2309. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2310. asml.remove(hp2);
  2311. hp2.free;
  2312. continue;
  2313. end;
  2314. end;
  2315. end;
  2316. end;
  2317. end;
  2318. end;
  2319. end;
  2320. p := tai(p.next)
  2321. end;
  2322. end;
  2323. begin
  2324. casmoptimizer:=TCpuAsmOptimizer;
  2325. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  2326. End.