aoptcpu.pas 118 KB

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