aoptcpu.pas 115 KB

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