aoptcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. Interface
  21. uses cpubase, cgbase, aasmtai, aopt, aoptcpub;
  22. Type
  23. TCpuAsmOptimizer = class(TAsmOptimizer)
  24. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  25. function RegInInstruction(Reg: TRegister; p1: tai): Boolean; override;
  26. { uses the same constructor as TAopObj }
  27. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  28. procedure PeepHoleOptPass2;override;
  29. End;
  30. Implementation
  31. uses
  32. cutils,
  33. cpuinfo,
  34. aasmbase,aasmcpu,
  35. globals,globtype,
  36. cgutils;
  37. function CanBeCond(p : tai) : boolean;
  38. begin
  39. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  40. end;
  41. function RefsEqual(const r1, r2: treference): boolean;
  42. begin
  43. refsequal :=
  44. (r1.offset = r2.offset) and
  45. (r1.base = r2.base) and
  46. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  47. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  48. (r1.relsymbol = r2.relsymbol) and
  49. (r1.addressmode = r2.addressmode);
  50. end;
  51. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  52. begin
  53. result:=oper1.typ=oper2.typ;
  54. if result then
  55. case oper1.typ of
  56. top_const:
  57. Result:=oper1.val = oper2.val;
  58. top_reg:
  59. Result:=oper1.reg = oper2.reg;
  60. top_ref:
  61. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  62. else Result:=false;
  63. end
  64. end;
  65. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  66. begin
  67. result := (oper.typ = top_reg) and (oper.reg = reg);
  68. end;
  69. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  70. begin
  71. result :=
  72. (instr.typ = ait_instruction) and
  73. (taicpu(instr).opcode = op);
  74. end;
  75. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  76. begin
  77. If (p1.typ = ait_instruction) and (taicpu(p1).opcode in [A_MUL,A_MULS,A_FMUL,A_FMULS,A_FMULSU]) and
  78. ((getsupreg(reg)=RS_R0) or (getsupreg(reg)=RS_R1)) then
  79. Result:=true
  80. else
  81. Result:=inherited RegInInstruction(Reg, p1);
  82. end;
  83. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  84. var Next: tai; reg: TRegister): Boolean;
  85. begin
  86. Next:=Current;
  87. repeat
  88. Result:=GetNextInstruction(Next,Next);
  89. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  90. (is_calljmp(taicpu(Next).opcode));
  91. end;
  92. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  93. var
  94. hp1,hp2,hp3: tai;
  95. alloc, dealloc: tai_regalloc;
  96. i: integer;
  97. begin
  98. result := false;
  99. case p.typ of
  100. ait_instruction:
  101. begin
  102. case taicpu(p).opcode of
  103. A_LDI:
  104. begin
  105. { turn
  106. ldi reg0, imm
  107. cp reg1, reg0
  108. dealloc reg0
  109. into
  110. cpi reg1, imm
  111. }
  112. if (taicpu(p).ops=2) and
  113. (taicpu(p).oper[0]^.typ=top_reg) and
  114. (taicpu(p).oper[1]^.typ=top_const) and
  115. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  116. (hp1.typ=ait_instruction) and
  117. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  118. (taicpu(hp1).opcode=A_CP) and
  119. (taicpu(hp1).ops=2) and
  120. (taicpu(hp1).oper[1]^.typ=top_reg) and
  121. (getsupreg(taicpu(hp1).oper[0]^.reg) in [16..31]) and
  122. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  123. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  124. begin
  125. taicpu(hp1).opcode:=A_CPI;
  126. taicpu(hp1).loadconst(1, taicpu(p).oper[1]^.val);
  127. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  128. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  129. if assigned(alloc) and assigned(dealloc) then
  130. begin
  131. asml.Remove(alloc);
  132. alloc.Free;
  133. asml.Remove(dealloc);
  134. dealloc.Free;
  135. end;
  136. GetNextInstruction(p,hp1);
  137. asml.Remove(p);
  138. p.Free;
  139. p:=hp1;
  140. result:=true;
  141. end;
  142. end;
  143. A_STS:
  144. if (taicpu(p).oper[0]^.ref^.symbol=nil) and
  145. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  146. (getsupreg(taicpu(p).oper[0]^.ref^.base)=RS_NO) and
  147. (getsupreg(taicpu(p).oper[0]^.ref^.index)=RS_NO) and
  148. (taicpu(p).oper[0]^.ref^.addressmode=AM_UNCHANGED) and
  149. (taicpu(p).oper[0]^.ref^.offset>=32) and
  150. (taicpu(p).oper[0]^.ref^.offset<=95) then
  151. begin
  152. taicpu(p).opcode:=A_OUT;
  153. taicpu(p).loadconst(0,taicpu(p).oper[0]^.ref^.offset-32);
  154. end;
  155. A_LDS:
  156. if (taicpu(p).oper[1]^.ref^.symbol=nil) and
  157. (taicpu(p).oper[1]^.ref^.relsymbol=nil) and
  158. (getsupreg(taicpu(p).oper[1]^.ref^.base)=RS_NO) and
  159. (getsupreg(taicpu(p).oper[1]^.ref^.index)=RS_NO) and
  160. (taicpu(p).oper[1]^.ref^.addressmode=AM_UNCHANGED) and
  161. (taicpu(p).oper[1]^.ref^.offset>=32) and
  162. (taicpu(p).oper[1]^.ref^.offset<=95) then
  163. begin
  164. taicpu(p).opcode:=A_IN;
  165. taicpu(p).loadconst(1,taicpu(p).oper[1]^.ref^.offset-32);
  166. end;
  167. A_IN:
  168. if GetNextInstruction(p,hp1) then
  169. begin
  170. {
  171. in rX,Y
  172. ori rX,n
  173. out Y,rX
  174. into
  175. sbi rX,lg(n)
  176. }
  177. if MatchInstruction(hp1,A_ORI) and
  178. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  179. (PopCnt(byte(taicpu(hp1).oper[1]^.val))=1) and
  180. GetNextInstruction(hp1,hp2) and
  181. MatchInstruction(hp2,A_OUT) and
  182. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  183. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  184. begin
  185. taicpu(p).opcode:=A_SBI;
  186. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  187. taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val)-1);
  188. asml.Remove(hp1);
  189. hp1.Free;
  190. asml.Remove(hp2);
  191. hp2.Free;
  192. result:=true;
  193. end
  194. {
  195. in rX,Y
  196. andi rX,not(n)
  197. out Y,rX
  198. into
  199. cbi rX,lg(n)
  200. }
  201. else if MatchInstruction(hp1,A_ANDI) and
  202. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  203. (PopCnt(byte(not(taicpu(hp1).oper[1]^.val)))=1) and
  204. GetNextInstruction(hp1,hp2) and
  205. MatchInstruction(hp2,A_OUT) and
  206. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  207. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  208. begin
  209. taicpu(p).opcode:=A_CBI;
  210. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  211. taicpu(p).loadconst(1,BsrByte(not(taicpu(hp1).oper[1]^.val))-1);
  212. asml.Remove(hp1);
  213. hp1.Free;
  214. asml.Remove(hp2);
  215. hp2.Free;
  216. result:=true;
  217. end;
  218. end;
  219. A_CLR:
  220. begin
  221. { turn the common
  222. clr rX
  223. mov/ld rX, rY
  224. into
  225. mov/ld rX, rY
  226. }
  227. if (taicpu(p).ops=1) and
  228. (taicpu(p).oper[0]^.typ=top_reg) and
  229. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  230. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  231. (hp1.typ=ait_instruction) and
  232. (taicpu(hp1).opcode in [A_MOV,A_LD]) and
  233. (taicpu(hp1).ops>0) and
  234. (taicpu(hp1).oper[0]^.typ=top_reg) and
  235. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
  236. begin
  237. asml.Remove(p);
  238. p.Free;
  239. p:=hp1;
  240. result:=true;
  241. end
  242. { turn
  243. clr rX
  244. ...
  245. adc rY, rX
  246. into
  247. ...
  248. adc rY, r1
  249. }
  250. else if (taicpu(p).ops=1) and
  251. (taicpu(p).oper[0]^.typ=top_reg) and
  252. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  253. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  254. (hp1.typ=ait_instruction) and
  255. (taicpu(hp1).opcode in [A_ADC,A_SBC]) and
  256. (taicpu(hp1).ops=2) and
  257. (taicpu(hp1).oper[1]^.typ=top_reg) and
  258. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  259. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[0]^.reg) and
  260. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  261. begin
  262. taicpu(hp1).oper[1]^.reg:=NR_R1;
  263. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  264. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  265. if assigned(alloc) and assigned(dealloc) then
  266. begin
  267. asml.Remove(alloc);
  268. alloc.Free;
  269. asml.Remove(dealloc);
  270. dealloc.Free;
  271. end;
  272. GetNextInstruction(p,hp1);
  273. asml.Remove(p);
  274. p.free;
  275. p:=hp1;
  276. result:=true;
  277. end;
  278. end;
  279. A_PUSH:
  280. begin
  281. { turn
  282. push reg0
  283. push reg1
  284. pop reg3
  285. pop reg2
  286. into
  287. movw reg2,reg0
  288. }
  289. if (taicpu(p).ops=1) and
  290. (taicpu(p).oper[0]^.typ=top_reg) and
  291. GetNextInstruction(p,hp1) and
  292. (hp1.typ=ait_instruction) and
  293. (taicpu(hp1).opcode=A_PUSH) and
  294. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  295. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  296. GetNextInstruction(hp1,hp2) and
  297. (hp2.typ=ait_instruction) and
  298. (taicpu(hp2).opcode=A_POP) and
  299. GetNextInstruction(hp2,hp3) and
  300. (hp3.typ=ait_instruction) and
  301. (taicpu(hp3).opcode=A_POP) and
  302. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp3).oper[0]^.reg)+1) and
  303. ((getsupreg(taicpu(hp3).oper[0]^.reg) mod 2)=0) then
  304. begin
  305. taicpu(p).ops:=2;
  306. taicpu(p).opcode:=A_MOVW;
  307. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  308. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  309. asml.Remove(hp1);
  310. hp1.Free;
  311. asml.Remove(hp2);
  312. hp2.Free;
  313. asml.Remove(hp3);
  314. hp3.Free;
  315. result:=true;
  316. end;
  317. end;
  318. A_MOV:
  319. begin
  320. { turn
  321. mov reg0, reg1
  322. push reg0
  323. dealloc reg0
  324. into
  325. push reg1
  326. }
  327. if (taicpu(p).ops=2) and
  328. (taicpu(p).oper[0]^.typ = top_reg) and
  329. (taicpu(p).oper[1]^.typ = top_reg) and
  330. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  331. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  332. (hp1.typ = ait_instruction) and
  333. (taicpu(hp1).opcode in [A_PUSH,A_MOV,A_CP,A_CPC,A_ADD,A_SUB,A_EOR,A_AND,A_OR]) and
  334. RegInInstruction(taicpu(p).oper[0]^.reg, hp1) and
  335. (not RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1)) and
  336. {(taicpu(hp1).ops=1) and
  337. (taicpu(hp1).oper[0]^.typ = top_reg) and
  338. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and }
  339. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  340. begin
  341. for i := 0 to taicpu(hp1).ops-1 do
  342. if taicpu(hp1).oper[i]^.typ=top_reg then
  343. if taicpu(hp1).oper[i]^.reg=taicpu(p).oper[0]^.reg then
  344. taicpu(hp1).oper[i]^.reg:=taicpu(p).oper[1]^.reg;
  345. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  346. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  347. if assigned(alloc) and assigned(dealloc) then
  348. begin
  349. asml.Remove(alloc);
  350. alloc.Free;
  351. asml.Remove(dealloc);
  352. dealloc.Free;
  353. end;
  354. GetNextInstruction(p,hp1);
  355. asml.Remove(p);
  356. p.free;
  357. p:=hp1;
  358. result:=true;
  359. end
  360. { remove
  361. mov reg0,reg0
  362. }
  363. else if (taicpu(p).ops=2) and
  364. (taicpu(p).oper[0]^.typ = top_reg) and
  365. (taicpu(p).oper[1]^.typ = top_reg) and
  366. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  367. begin
  368. GetNextInstruction(p,hp1);
  369. asml.remove(p);
  370. p.free;
  371. p:=hp1;
  372. result:=true;
  373. end
  374. { fold
  375. mov reg2,reg0
  376. mov reg3,reg1
  377. to
  378. movw reg2,reg0
  379. }
  380. else if (CPUAVR_HAS_MOVW in cpu_capabilities[current_settings.cputype]) and
  381. (taicpu(p).ops=2) and
  382. (taicpu(p).oper[0]^.typ = top_reg) and
  383. (taicpu(p).oper[1]^.typ = top_reg) and
  384. getnextinstruction(p,hp1) and
  385. (hp1.typ = ait_instruction) and
  386. (taicpu(hp1).opcode = A_MOV) and
  387. (taicpu(hp1).ops=2) and
  388. (taicpu(hp1).oper[0]^.typ = top_reg) and
  389. (taicpu(hp1).oper[1]^.typ = top_reg) and
  390. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  391. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  392. ((getsupreg(taicpu(p).oper[1]^.reg) mod 2)=0) and
  393. (getsupreg(taicpu(hp1).oper[1]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)+1) then
  394. begin
  395. alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg,tai(hp1.Previous));
  396. if assigned(alloc) then
  397. begin
  398. asml.Remove(alloc);
  399. asml.InsertBefore(alloc,p);
  400. end;
  401. taicpu(p).opcode:=A_MOVW;
  402. asml.remove(hp1);
  403. hp1.free;
  404. result:=true;
  405. end
  406. {
  407. This removes the first mov from
  408. mov rX,...
  409. mov rX,...
  410. }
  411. else if taicpu(hp1).opcode=A_MOV then
  412. while (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_MOV) and
  413. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  414. { don't remove the first mov if the second is a mov rX,rX }
  415. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) do
  416. begin
  417. asml.remove(p);
  418. p.free;
  419. p:=hp1;
  420. GetNextInstruction(hp1,hp1);
  421. result:=true;
  422. if not assigned(hp1) then
  423. break;
  424. end;
  425. end;
  426. end;
  427. end;
  428. end;
  429. end;
  430. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  431. begin
  432. end;
  433. begin
  434. casmoptimizer:=TCpuAsmOptimizer;
  435. End.