aoptcpu.pas 124 KB

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