aoptcpu.pas 111 KB

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