regclass.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* Compute register class preferences for pseudo-registers.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This file contains two passes of the compiler: reg_scan and reg_class. */
  18. #include "config.h"
  19. #include "rtl.h"
  20. #include "regs.h"
  21. #include "insn-config.h"
  22. #include "recog.h"
  23. #define max(A,B) ((A) > (B) ? (A) : (B))
  24. #define min(A,B) ((A) < (B) ? (A) : (B))
  25. /* savings[R].savings[CL] is the amount saved by putting register R
  26. in class CL. This data is used within `regclass' and freed
  27. when it is finished. */
  28. struct savings
  29. {
  30. short savings[N_REG_CLASSES];
  31. };
  32. static struct savings *savings;
  33. /* (enum reg_class) prefclass[R] is the preferred class for register R.
  34. This is available after `regclass' is run. */
  35. static char *prefclass;
  36. static enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES]
  37. = REG_CLASS_SUBUNION;
  38. static enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES]
  39. = REG_CLASS_SUBCLASSES;
  40. /* Indexed by pseudo register number, gives uid of first insn using the reg
  41. (as of the time reg_scan is called). */
  42. short *regno_first_uid;
  43. /* Indexed by pseudo register number, gives uid of last insn using the reg
  44. (as of the time reg_scan is called). */
  45. short *regno_last_uid;
  46. void reg_class_record ();
  47. void record_address_regs ();
  48. void reg_scan_mark_refs ();
  49. /* Return the reg_class in which pseudo reg number REGNO is best allocated.
  50. This function is sometimes called before the info has been computed.
  51. When that happens, just return GENERAL_REGS, which is innocuous. */
  52. enum reg_class
  53. reg_preferred_class (regno)
  54. int regno;
  55. {
  56. if (prefclass == 0)
  57. return GENERAL_REGS;
  58. return (enum reg_class) prefclass[regno];
  59. }
  60. /* This is a pass of the compiler that scans all instructions
  61. and calculates the preferred class for each pseudo-register.
  62. This information can be accessed later by calling `reg_preferred_class'.
  63. This pass comes just before local register allocation. */
  64. regclass (f, nregs)
  65. rtx f;
  66. int nregs;
  67. {
  68. #ifdef REGISTER_CONSTRAINTS
  69. register rtx insn;
  70. register int i;
  71. init_recog ();
  72. /* Zero out our accumulation of the cost of each class for each reg. */
  73. savings = (struct savings *) alloca (nregs * sizeof (struct savings));
  74. bzero (savings, nregs * sizeof (struct savings));
  75. /* Scan the instructions and record each time it would
  76. save code to put a certain register in a certain class. */
  77. for (insn = f; insn; insn = NEXT_INSN (insn))
  78. if ((GET_CODE (insn) == INSN
  79. && GET_CODE (PATTERN (insn)) != USE
  80. && GET_CODE (PATTERN (insn)) != CLOBBER
  81. && GET_CODE (PATTERN (insn)) != ASM_INPUT)
  82. || (GET_CODE (insn) == JUMP_INSN
  83. && GET_CODE (PATTERN (insn)) != ADDR_VEC
  84. && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
  85. || GET_CODE (insn) == CALL_INSN)
  86. {
  87. int insn_code_number = recog_memoized (insn);
  88. insn_extract (insn);
  89. for (i = insn_n_operands[insn_code_number] - 1; i >= 0; i--)
  90. reg_class_record (recog_operand[i],
  91. &insn_operand_constraint[insn_code_number][i]);
  92. }
  93. /* Now for each register look at how desirable each class is
  94. and find which class is preferred. Store that in `prefclass[REGNO]'. */
  95. prefclass = (char *) oballoc (nregs);
  96. for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
  97. {
  98. register int best_savings = 0;
  99. enum reg_class best = ALL_REGS;
  100. /* This is an enum reg_class, but we call it an int
  101. to save lots of casts. */
  102. register int class;
  103. register struct savings *p = &savings[i];
  104. for (class = (int) ALL_REGS - 1; class > 0; class--)
  105. {
  106. if (p->savings[class] > best_savings)
  107. {
  108. best_savings = p->savings[class];
  109. best = (enum reg_class) class;
  110. }
  111. else if (p->savings[class] == best_savings)
  112. {
  113. best = reg_class_subunion[(int)best][class];
  114. }
  115. }
  116. #if 0
  117. /* Note that best_savings is twice number of places something
  118. is saved. */
  119. if ((best_savings - p->savings[(int) GENERAL_REGS]) * 5 < reg_n_refs[i])
  120. prefclass[i] = (char) GENERAL_REGS;
  121. else
  122. prefclass[i] = (char) best;
  123. #else
  124. prefclass[i] = (char) best;
  125. #endif
  126. }
  127. #endif /* REGISTER_CONSTRAINTS */
  128. }
  129. #ifdef REGISTER_CONSTRAINTS
  130. /* Scan an operand OP to which the constraint *CONSTRAINT_LOC should apply
  131. and record the preferred register classes from the constraint for OP
  132. if OP is a register. If OP is a memory reference, record suitable
  133. preferences for registers used in the address.
  134. We can deduce both the insn code number and which operand in the insn
  135. this is supposed to be from the position of CONSTRAINT_LOC
  136. in the table of constraints. */
  137. void
  138. reg_class_record (op, constraint_loc)
  139. rtx op;
  140. char **constraint_loc;
  141. {
  142. char *constraint = *constraint_loc;
  143. register char *p;
  144. register enum reg_class class = NO_REGS;
  145. char *next = 0;
  146. int insn_code = (constraint_loc - insn_operand_constraint[0]) / MAX_RECOG_OPERANDS;
  147. while (1)
  148. {
  149. if (GET_CODE (op) == VOLATILE
  150. || GET_CODE (op) == UNCHANGING)
  151. op = XEXP (op, 0);
  152. else if (GET_CODE (op) == SUBREG)
  153. op = SUBREG_REG (op);
  154. else break;
  155. }
  156. /* Memory reference: scan the address. */
  157. if (GET_CODE (op) == MEM)
  158. record_address_regs (XEXP (op, 0), 2, 0);
  159. if (GET_CODE (op) != REG)
  160. {
  161. /* If the constraint says the operand is supposed to BE an address,
  162. scan it as one. */
  163. if (constraint != 0 && constraint[0] == 'p')
  164. record_address_regs (op, 2, 0);
  165. return;
  166. }
  167. /* Operand is a register: examine the constraint for specified classes. */
  168. for (p = constraint; *p || next; p++)
  169. {
  170. if (*p == 0)
  171. {
  172. p = next;
  173. next = 0;
  174. }
  175. switch (*p)
  176. {
  177. case '=':
  178. case '+':
  179. case '?':
  180. case '#':
  181. case '!':
  182. case '%':
  183. case 'F':
  184. case 'G':
  185. case 'H':
  186. case 'i':
  187. case 's':
  188. case 'm':
  189. case 'o':
  190. case 'p':
  191. case ',':
  192. break;
  193. /* * means ignore following letter
  194. when choosing register preferences. */
  195. case '*':
  196. p++;
  197. break;
  198. case 'g':
  199. case 'r':
  200. if (GENERAL_REGS == ALL_REGS)
  201. return;
  202. class = GENERAL_REGS;
  203. break;
  204. case '0':
  205. case '1':
  206. case '2':
  207. case '3':
  208. case '4':
  209. /* If constraint says "match another operand",
  210. use that operand's constraint to choose preferences. */
  211. next = insn_operand_constraint[insn_code][*p - '0'];
  212. break;
  213. default:
  214. class
  215. = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (*p)];
  216. }
  217. }
  218. {
  219. register int i;
  220. register struct savings *pp;
  221. register enum reg_class class1;
  222. pp = &savings[REGNO (op)];
  223. /* Increment the savings for this reg
  224. for each class contained in the one the constraint asks for. */
  225. if (class != NO_REGS && class != ALL_REGS)
  226. {
  227. pp->savings[(int) class] += 2;
  228. for (i = 0; ; i++)
  229. {
  230. class1 = reg_class_subclasses[(int)class][i];
  231. if (class1 == LIM_REG_CLASSES)
  232. break;
  233. pp->savings[(int) class1] += 2;
  234. }
  235. }
  236. }
  237. }
  238. /* Record the pseudo registers we must reload into hard registers
  239. in a subexpression of a memory address, X.
  240. BCOST is the cost if X is a register and it fails to be in BASE_REG_CLASS.
  241. ICOST is the cost if it fails to be in INDEX_REG_CLASS. */
  242. void
  243. record_address_regs (x, bcost, icost)
  244. rtx x;
  245. int bcost, icost;
  246. {
  247. register RTX_CODE code = GET_CODE (x);
  248. switch (code)
  249. {
  250. case CONST_INT:
  251. case CONST:
  252. case CC0:
  253. case PC:
  254. case SYMBOL_REF:
  255. case LABEL_REF:
  256. return;
  257. case PLUS:
  258. /* When we have an address that is a sum,
  259. we must determine whether registers are "base" or "index" regs.
  260. If there is a sum of two registers, we must choose one to be
  261. the "base". Luckily, we can use the REGNO_POINTER_FLAG
  262. to make a good choice most of the time. */
  263. {
  264. register RTX_CODE code0 = GET_CODE (XEXP (x, 0));
  265. register RTX_CODE code1 = GET_CODE (XEXP (x, 1));
  266. int icost0 = 0;
  267. int icost1 = 0;
  268. int suppress1 = 0;
  269. int suppress0 = 0;
  270. if (code0 == MULT || code1 == MEM)
  271. icost0 = 2;
  272. else if (code1 == MULT || code0 == MEM)
  273. icost1 = 2;
  274. else if (code0 == CONST_INT)
  275. suppress0 = 1;
  276. else if (code1 == CONST_INT)
  277. suppress1 = 1;
  278. else if (code0 == REG && code1 == REG)
  279. {
  280. if (REGNO_POINTER_FLAG (REGNO (XEXP (x, 0))))
  281. icost1 = 2;
  282. else if (REGNO_POINTER_FLAG (REGNO (XEXP (x, 1))))
  283. icost0 = 2;
  284. else
  285. icost0 = icost1 = 1;
  286. }
  287. else if (code0 == REG)
  288. {
  289. if (code1 == PLUS
  290. && ! REGNO_POINTER_FLAG (REGNO (XEXP (x, 0))))
  291. icost0 = 2;
  292. else
  293. REGNO_POINTER_FLAG (REGNO (XEXP (x, 0))) = 1;
  294. }
  295. else if (code1 == REG)
  296. {
  297. if (code0 == PLUS
  298. && ! REGNO_POINTER_FLAG (REGNO (XEXP (x, 1))))
  299. icost1 = 2;
  300. else
  301. REGNO_POINTER_FLAG (REGNO (XEXP (x, 1))) = 1;
  302. }
  303. /* ICOST0 determines whether we are treating operand 0
  304. as a base register or as an index register.
  305. SUPPRESS0 nonzero means it isn't a register at all.
  306. ICOST1 and SUPPRESS1 are likewise for operand 1. */
  307. if (! suppress0)
  308. record_address_regs (XEXP (x, 0), 2 - icost0, icost0);
  309. if (! suppress1)
  310. record_address_regs (XEXP (x, 1), 2 - icost1, icost1);
  311. }
  312. break;
  313. case POST_INC:
  314. case PRE_INC:
  315. case POST_DEC:
  316. case PRE_DEC:
  317. /* Double the importance of a pseudo register that is incremented
  318. or decremented, since it would take two extra insns
  319. if it ends up in the wrong place. */
  320. record_address_regs (XEXP (x, 0), 2 * bcost, 2 * icost);
  321. break;
  322. case REG:
  323. {
  324. register struct savings *pp;
  325. register enum reg_class class, class1;
  326. pp = &savings[REGNO (x)];
  327. /* We have an address (or part of one) that is just one register. */
  328. /* Record BCOST worth of savings for classes contained
  329. in BASE_REG_CLASS. */
  330. class = BASE_REG_CLASS;
  331. if (class != NO_REGS && class != ALL_REGS)
  332. {
  333. register int i;
  334. pp->savings[(int) class] += bcost;
  335. for (i = 0; ; i++)
  336. {
  337. class1 = reg_class_subclasses[(int)class][i];
  338. if (class1 == LIM_REG_CLASSES)
  339. break;
  340. pp->savings[(int) class1] += bcost;
  341. }
  342. }
  343. /* Record ICOST worth of savings for classes contained
  344. in INDEX_REG_CLASS. */
  345. class = INDEX_REG_CLASS;
  346. if (class != NO_REGS && class != ALL_REGS)
  347. {
  348. register int i;
  349. pp->savings[(int) class] += icost;
  350. for (i = 0; ; i++)
  351. {
  352. class1 = reg_class_subclasses[(int)class][i];
  353. if (class1 == LIM_REG_CLASSES)
  354. break;
  355. pp->savings[(int) class1] += icost;
  356. }
  357. }
  358. }
  359. break;
  360. default:
  361. {
  362. register char *fmt = GET_RTX_FORMAT (code);
  363. register int i;
  364. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  365. if (fmt[i] == 'e')
  366. record_address_regs (XEXP (x, i), bcost, icost);
  367. }
  368. }
  369. }
  370. #endif /* REGISTER_CONSTRAINTS */
  371. /* This is a pass of the compiler run before cse.
  372. It sets up the vectors regno_first_uid, regno_last_uid. */
  373. reg_scan (f, nregs)
  374. rtx f;
  375. int nregs;
  376. {
  377. register int i;
  378. register rtx last, insn;
  379. /* This prevents dump_flow_info from losing if called
  380. before regclass is run. */
  381. prefclass = 0;
  382. regno_first_uid = (short *) oballoc (nregs * sizeof (short));
  383. bzero (regno_first_uid, nregs * sizeof (short));
  384. regno_last_uid = (short *) oballoc (nregs * sizeof (short));
  385. bzero (regno_last_uid, nregs * sizeof (short));
  386. for (insn = f; insn; insn = NEXT_INSN (insn))
  387. if (GET_CODE (insn) == INSN
  388. || GET_CODE (insn) == CALL_INSN
  389. || GET_CODE (insn) == JUMP_INSN)
  390. reg_scan_mark_refs (PATTERN (insn), INSN_UID (insn));
  391. }
  392. void
  393. reg_scan_mark_refs (x, uid)
  394. rtx x;
  395. int uid;
  396. {
  397. register RTX_CODE code = GET_CODE (x);
  398. switch (code)
  399. {
  400. case CONST_INT:
  401. case CONST:
  402. case CONST_DOUBLE:
  403. case CC0:
  404. case PC:
  405. case SYMBOL_REF:
  406. case LABEL_REF:
  407. return;
  408. case REG:
  409. {
  410. register int regno = REGNO (x);
  411. regno_last_uid[regno] = uid;
  412. if (regno_first_uid[regno] == 0)
  413. regno_first_uid[regno] = uid;
  414. }
  415. break;
  416. default:
  417. {
  418. register char *fmt = GET_RTX_FORMAT (code);
  419. register int i;
  420. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  421. {
  422. if (fmt[i] == 'e')
  423. reg_scan_mark_refs (XEXP (x, i), uid);
  424. else if (fmt[i] == 'E')
  425. {
  426. register int j;
  427. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  428. reg_scan_mark_refs (XVECEXP (x, i, j), uid);
  429. }
  430. }
  431. }
  432. }
  433. }