aoptcpu.pas 123 KB

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