recog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* Subroutines used by or related to instruction recognition.
  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. #include "config.h"
  18. #include "rtl.h"
  19. #include <stdio.h>
  20. #include "insn-config.h"
  21. #include "recog.h"
  22. #include "regs.h"
  23. #include "hard-reg-set.h"
  24. rtx recog_addr_dummy;
  25. /* Initialize data used by the function `recog'.
  26. This must be called once in the compilation of a function
  27. before any insn recognition may be done in the function. */
  28. void
  29. init_recog (constrain)
  30. int constrain;
  31. {
  32. recog_addr_dummy = gen_rtx (MEM, VOIDmode, 0);
  33. }
  34. /* Try recognizing the instruction INSN,
  35. and return the code number that results.
  36. Remeber the code so that repeated calls do not
  37. need to spend the time for actual rerecognition.
  38. This function is the normal interface to instruction recognition.
  39. The automatically-generated function `recog' is called
  40. only through this one. */
  41. int
  42. recog_memoized (insn)
  43. rtx insn;
  44. {
  45. if (INSN_CODE (insn) < 0)
  46. INSN_CODE (insn) = recog (PATTERN (insn), insn);
  47. return INSN_CODE (insn);
  48. }
  49. /* Return 1 if the insn following INSN does not contain
  50. any ordered tests applied to the condition codes.
  51. EQ and NE tests do not count. */
  52. int
  53. next_insn_tests_no_inequality (insn)
  54. rtx insn;
  55. {
  56. register rtx next = NEXT_INSN (insn);
  57. static int inequality_comparisons_p ();
  58. return ((GET_CODE (next) == JUMP_INSN
  59. || GET_CODE (next) == INSN
  60. || GET_CODE (next) == CALL_INSN)
  61. && ! inequality_comparisons_p (PATTERN (next)));
  62. }
  63. static int
  64. inequality_comparisons_p (x)
  65. rtx x;
  66. {
  67. register char *fmt;
  68. register int len, i;
  69. register enum rtx_code code = GET_CODE (x);
  70. switch (code)
  71. {
  72. case REG:
  73. case PC:
  74. case CC0:
  75. case CONST_INT:
  76. case CONST_DOUBLE:
  77. case CONST:
  78. case LABEL_REF:
  79. case SYMBOL_REF:
  80. return 0;
  81. case LT:
  82. case LTU:
  83. case GT:
  84. case GTU:
  85. case LE:
  86. case LEU:
  87. case GE:
  88. case GEU:
  89. return (XEXP (x, 0) == cc0_rtx || XEXP (x, 1) == cc0_rtx);
  90. }
  91. len = GET_RTX_LENGTH (code);
  92. fmt = GET_RTX_FORMAT (code);
  93. for (i = 0; i < len; i++)
  94. {
  95. if (fmt[i] == 'e')
  96. {
  97. if (inequality_comparisons_p (XEXP (x, i)))
  98. return 1;
  99. }
  100. else if (fmt[i] == 'E')
  101. {
  102. register int j;
  103. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  104. if (inequality_comparisons_p (XEXP (x, i)))
  105. return 1;
  106. }
  107. }
  108. return 0;
  109. }
  110. /* Return 1 if OP is a valid general operand for machine mode MODE.
  111. This is either a register reference, a memory reference,
  112. or a constant. In the case of a memory reference, the address
  113. is checked for general validity for the target machine.
  114. Register and memory references must have mode MODE in order to be valid,
  115. but some constants have no machine mode and are valid for any mode.
  116. If MODE is VOIDmode, OP is checked for validity for whatever mode
  117. it has.
  118. The main use of this function is as a predicate in match_operand
  119. expressions in the machine description. */
  120. int
  121. general_operand (op, mode)
  122. register rtx op;
  123. enum machine_mode mode;
  124. {
  125. register enum rtx_code code = GET_CODE (op);
  126. if (CONSTANT_ADDRESS_P (op))
  127. return 1;
  128. if (mode == VOIDmode)
  129. mode = GET_MODE (op);
  130. else if (GET_MODE (op) != mode)
  131. return 0;
  132. while (code == SUBREG)
  133. {
  134. op = SUBREG_REG (op);
  135. code = GET_CODE (op);
  136. }
  137. if (code == REG)
  138. return 1;
  139. if (code == CONST_DOUBLE)
  140. return 1;
  141. if (code == MEM)
  142. {
  143. register rtx y = XEXP (op, 0);
  144. GO_IF_LEGITIMATE_ADDRESS (mode, y, win);
  145. }
  146. return 0;
  147. win:
  148. return 1;
  149. }
  150. /* Return 1 if OP is a valid memory address for a memory reference
  151. of mode MODE.
  152. The main use of this function is as a predicate in match_operand
  153. expressions in the machine description. */
  154. int
  155. address_operand (op, mode)
  156. register rtx op;
  157. enum machine_mode mode;
  158. {
  159. GO_IF_LEGITIMATE_ADDRESS (mode, op, win);
  160. return 0;
  161. win:
  162. return 1;
  163. }
  164. /* Return 1 if OP is a valid immediate operand for mode MODE.
  165. The main use of this function is as a predicate in match_operand
  166. expressions in the machine description. */
  167. int
  168. immediate_operand (op, mode)
  169. register rtx op;
  170. enum machine_mode mode;
  171. {
  172. return (CONSTANT_ADDRESS_P (op)
  173. || (GET_CODE (op) == CONST_DOUBLE
  174. && GET_MODE (op) == mode));
  175. }
  176. /* Return 1 if OP is a valid operand that stands for pushing a
  177. value of mode MODE onto the stack.
  178. The main use of this function is as a predicate in match_operand
  179. expressions in the machine description. */
  180. int
  181. push_operand (op, mode)
  182. rtx op;
  183. enum machine_mode mode;
  184. {
  185. if (GET_CODE (op) != MEM)
  186. return 0;
  187. if (GET_MODE (op) != mode)
  188. return 0;
  189. op = XEXP (op, 0);
  190. #ifdef STACK_GROWS_DOWNWARD
  191. if (GET_CODE (op) != PRE_DEC)
  192. return 0;
  193. #else
  194. if (GET_CODE (op) != PRE_INC)
  195. return 0;
  196. #endif
  197. return REGNO (XEXP (op, 0)) == STACK_POINTER_REGNUM;
  198. }
  199. /* Return 1 if OP is a valid memory reference with mode MODE,
  200. including a valid address.
  201. The main use of this function is as a predicate in match_operand
  202. expressions in the machine description. */
  203. int
  204. memory_operand (op, mode)
  205. register rtx op;
  206. enum machine_mode mode;
  207. {
  208. return GET_CODE (op) == MEM && general_operand (op, mode);
  209. }
  210. extern rtx plus_constant ();
  211. extern rtx copy_rtx ();
  212. /* Given an rtx *P, if it is a sum containing an integer constant term,
  213. return the location (type rtx *) of the pointer to that constant term.
  214. Otherwise, return a null pointer. */
  215. static rtx *
  216. find_constant_term_loc (p)
  217. rtx *p;
  218. {
  219. register rtx *tem;
  220. register RTX_CODE code = GET_CODE (*p);
  221. /* If *P IS such a constant term, P is its location. */
  222. if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
  223. || code == CONST)
  224. return p;
  225. /* Otherwise, if not a sum, it has no constant term. */
  226. if (GET_CODE (*p) != PLUS)
  227. return 0;
  228. /* If one of the summands is constant, return its location. */
  229. if (XEXP (*p, 0) && CONSTANT_ADDRESS_P (XEXP (*p, 0))
  230. && XEXP (*p, 1) && CONSTANT_ADDRESS_P (XEXP (*p, 1)))
  231. return p;
  232. /* Otherwise, check each summand for containing a constant term. */
  233. if (XEXP (*p, 0) != 0)
  234. {
  235. tem = find_constant_term_loc (&XEXP (*p, 0));
  236. if (tem != 0)
  237. return tem;
  238. }
  239. if (XEXP (*p, 1) != 0)
  240. {
  241. tem = find_constant_term_loc (&XEXP (*p, 1));
  242. if (tem != 0)
  243. return tem;
  244. }
  245. return 0;
  246. }
  247. /* Return 1 if OP is a memory reference
  248. whose address remains valid after the addition
  249. of a positive integer less than the
  250. size of the object being referenced.
  251. We assume that the original address is valid and do not check it. */
  252. int
  253. offsetable_address_p (op)
  254. rtx op;
  255. {
  256. enum machine_mode mode = GET_MODE (op);
  257. if (GET_CODE (op) == MEM)
  258. {
  259. register rtx y = XEXP (op, 0);
  260. register RTX_CODE ycode = GET_CODE (y);
  261. register rtx z;
  262. rtx y1 = y;
  263. rtx *y2;
  264. if (CONSTANT_ADDRESS_P (y))
  265. return 1;
  266. /* If the expression contains a constant term,
  267. see if it remains valid when max possible offset is added. */
  268. if ((GET_CODE (y) == PLUS) && (y2 = find_constant_term_loc (&y1)))
  269. {
  270. int old = INTVAL (y1 = *y2);
  271. int good = 1;
  272. INTVAL (y1) += GET_MODE_SIZE (mode) - 1;
  273. GO_IF_LEGITIMATE_ADDRESS (mode, y, win1);
  274. good = 0;
  275. win1:
  276. /* In any case, restore old contents of memory. */
  277. INTVAL (y1) = old;
  278. return good;
  279. }
  280. /* The offset added here is chosen as the maximum offset that
  281. any instruction could need to add when operating on something
  282. of the specified mode. We assume that if Y and Y+c are
  283. valid addresses then so is Y+d for all 0<d<c. */
  284. z = plus_constant (y, GET_MODE_SIZE (mode) - 1);
  285. GO_IF_LEGITIMATE_ADDRESS (mode, z, win);
  286. return 0;
  287. win:
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. /* Given an operand OP that is a valid memory reference
  293. which satisfies offsetable_address_p,
  294. return a new memory reference whose address has been adjusted by OFFSET.
  295. OFFSET should be positive and less than the size of the object referenced.
  296. */
  297. rtx
  298. adj_offsetable_operand (op, offset)
  299. rtx op;
  300. int offset;
  301. {
  302. register enum rtx_code code = GET_CODE (op);
  303. if (code == MEM)
  304. {
  305. register rtx y = XEXP (op, 0);
  306. if (CONSTANT_ADDRESS_P (y))
  307. return gen_rtx (MEM, GET_MODE (op), plus_constant (y, offset));
  308. if (GET_CODE (y) == PLUS)
  309. {
  310. rtx z = y;
  311. register rtx *const_loc;
  312. op = copy_rtx (op);
  313. z = XEXP (op, 0);
  314. const_loc = find_constant_term_loc (&z);
  315. if (const_loc)
  316. {
  317. *const_loc = plus_constant (*const_loc, offset);
  318. return op;
  319. }
  320. }
  321. return gen_rtx (MEM, GET_MODE (op), plus_constant (y, offset));
  322. }
  323. abort ();
  324. }
  325. #ifdef REGISTER_CONSTRAINTS
  326. /* Check the operands of an insn (found in recog_operands)
  327. against the insn's operand constraints (found via INSN_CODE_NUM)
  328. and return 1 if they are valid.
  329. This is used by final, and exists only for error checking,
  330. since reload should already have checked the constraints
  331. and altered the code as necessary to make it valid. */
  332. int
  333. constrain_operands (insn_code_num)
  334. int insn_code_num;
  335. {
  336. char *constraints[MAX_RECOG_OPERANDS];
  337. register int c;
  338. int noperands = insn_n_operands[insn_code_num];
  339. if (noperands == 0)
  340. return 1;
  341. for (c = 0; c < noperands; c++)
  342. constraints[c] = insn_operand_constraint[insn_code_num][c];
  343. while (*constraints[0])
  344. {
  345. register int opno;
  346. int lose = 0;
  347. for (opno = 0; opno < noperands; opno++)
  348. {
  349. register rtx op = recog_operand[opno];
  350. register char *p = constraints[opno];
  351. int win = 0;
  352. int offset = 0;
  353. while (GET_CODE (op) == SUBREG)
  354. {
  355. offset += SUBREG_WORD (op);
  356. op = SUBREG_REG (op);
  357. }
  358. while (*p && (c = *p++) != ',')
  359. switch (c)
  360. {
  361. case '=':
  362. case '+':
  363. case '?':
  364. case '#':
  365. case '!':
  366. case '*':
  367. case '%':
  368. break;
  369. case '0':
  370. case '1':
  371. case '2':
  372. case '3':
  373. case '4':
  374. /* This operand must be the same as a previous one. */
  375. /* This kind of constraint is used for instructions such
  376. as add when they take only two operands. */
  377. if (rtx_equal_p (recog_operand[opno], recog_operand[c - '0']))
  378. win = 1;
  379. break;
  380. case 'p':
  381. /* p is used for address_operands, and everything
  382. that must be checked was checked already. */
  383. win = 1;
  384. break;
  385. /* No need to check general_operand again;
  386. it was done in insn-recog.c. */
  387. case 'g':
  388. /* Anything goes unless it is a REG and really has a hard reg
  389. but the hard reg is not in the class GENERAL_REGS. */
  390. if (GENERAL_REGS == ALL_REGS
  391. || GET_CODE (op) != REG
  392. || (REGNO (op) >= FIRST_PSEUDO_REGISTER
  393. && reg_renumber[REGNO (op)] < 0)
  394. || reg_renumbered_fits_class_p (op, GENERAL_REGS, offset))
  395. win = 1;
  396. break;
  397. case 'r':
  398. if (GET_CODE (op) == REG
  399. && (GENERAL_REGS == ALL_REGS
  400. || reg_renumbered_fits_class_p (op, GENERAL_REGS,
  401. offset)))
  402. win = 1;
  403. break;
  404. case 'm':
  405. if (GET_CODE (op) == MEM)
  406. win = 1;
  407. break;
  408. case '<':
  409. if (GET_CODE (op) == MEM
  410. && (GET_CODE (XEXP (op, 0)) == PRE_DEC
  411. || GET_CODE (XEXP (op, 0)) == POST_DEC))
  412. win = 1;
  413. break;
  414. case '>':
  415. if (GET_CODE (op) == MEM
  416. && (GET_CODE (XEXP (op, 0)) == PRE_INC
  417. || GET_CODE (XEXP (op, 0)) == POST_INC))
  418. win = 1;
  419. break;
  420. case 'F':
  421. if (GET_CODE (op) == CONST_DOUBLE)
  422. win = 1;
  423. break;
  424. case 'G':
  425. case 'H':
  426. if (GET_CODE (op) == CONST_DOUBLE
  427. && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
  428. win = 1;
  429. break;
  430. case 's':
  431. if (GET_CODE (op) == CONST_INT)
  432. break;
  433. case 'i':
  434. if (CONSTANT_ADDRESS_P (op))
  435. win = 1;
  436. break;
  437. case 'I':
  438. case 'J':
  439. case 'K':
  440. case 'L':
  441. case 'M':
  442. if (GET_CODE (op) == CONST_INT
  443. && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
  444. win = 1;
  445. break;
  446. case 'o':
  447. if (offsetable_address_p (op))
  448. win = 1;
  449. break;
  450. default:
  451. if (GET_CODE (op) == REG
  452. && reg_renumbered_fits_class_p (op,
  453. REG_CLASS_FROM_LETTER (c),
  454. offset))
  455. win = 1;
  456. }
  457. constraints[opno] = p;
  458. /* If this operand did not win somehow,
  459. this alternative loses. */
  460. if (! win)
  461. lose = 1;
  462. }
  463. /* This alternative won; the operands are ok. */
  464. if (! lose)
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. /* Return 1 iff OPERAND (assumed to be a REG rtx)
  470. is a hard reg in class CLASS when its regno is offsetted by OFFSET,
  471. or is a pseudo reg allocated into such a hard reg.
  472. If REG occupies multiple hard regs, all of them must by in CLASS. */
  473. int
  474. reg_renumbered_fits_class_p (operand, class, offset)
  475. rtx operand;
  476. register enum reg_class class;
  477. int offset;
  478. {
  479. static HARD_REG_SET class_contents[] = REG_CLASS_CONTENTS;
  480. if (GET_CODE (operand) == REG)
  481. {
  482. register int regno = REGNO (operand);
  483. if (reg_renumber[regno] >= 0)
  484. regno = reg_renumber[regno];
  485. if (regno < FIRST_PSEUDO_REGISTER
  486. && TEST_HARD_REG_BIT (class_contents[(int) class], regno + offset))
  487. {
  488. register int sr;
  489. regno += offset;
  490. for (sr = HARD_REGNO_NREGS (regno, GET_MODE (operand)) - 1;
  491. sr > 0; sr--)
  492. if (! TEST_HARD_REG_BIT (class_contents[(int) class], regno + sr))
  493. break;
  494. return sr == 0;
  495. }
  496. }
  497. return 0;
  498. }
  499. #endif /* REGISTER_CONSTRAINTS */