aoptcpu.pas 123 KB

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