explow.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  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 "tree.h"
  20. #include "expr.h"
  21. /* Return an rtx for the sum of X and the integer C. */
  22. rtx
  23. plus_constant (x, c)
  24. register rtx x;
  25. register int c;
  26. {
  27. register RTX_CODE code = GET_CODE (x);
  28. register enum machine_mode mode = GET_MODE (x);
  29. int all_constant = 0;
  30. if (c == 0)
  31. return x;
  32. if (code == CONST_INT)
  33. return gen_rtx (CONST_INT, VOIDmode, (INTVAL (x) + c));
  34. /* If adding to something entirely constant, set a flag
  35. so that we can add a CONST around the result. */
  36. if (code == CONST)
  37. {
  38. x = XEXP (x, 0);
  39. all_constant = 1;
  40. }
  41. else if (code == SYMBOL_REF || code == LABEL_REF)
  42. all_constant = 1;
  43. /* The interesting case is adding the integer to a sum.
  44. Look for constant term in the sum and combine
  45. with C. For an integer constant term, we make a combined
  46. integer. For a constant term that is not an explicit integer,
  47. we cannot really combine, but group them together anyway. */
  48. if (GET_CODE (x) == PLUS)
  49. {
  50. if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  51. {
  52. c += INTVAL (XEXP (x, 0));
  53. x = XEXP (x, 1);
  54. }
  55. else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  56. {
  57. c += INTVAL (XEXP (x, 1));
  58. x = XEXP (x, 0);
  59. }
  60. else if (GET_CODE (XEXP (x, 0)) == CONST)
  61. {
  62. return gen_rtx (PLUS, mode,
  63. plus_constant (XEXP (x, 0), c),
  64. XEXP (x, 1));
  65. }
  66. else if (GET_CODE (XEXP (x, 1)) == CONST)
  67. {
  68. return gen_rtx (PLUS, mode,
  69. XEXP (x, 0),
  70. plus_constant (XEXP (x, 1), c));
  71. }
  72. #ifdef OLD_INDEXING
  73. /* Detect adding a constant to an indexed address
  74. of the form (PLUS (MULT (REG) (CONST)) regs-and-constants).
  75. Keep the (MULT ...) at the top level of addition so that
  76. the result is still suitable for indexing and constants
  77. are combined. */
  78. else if (GET_CODE (XEXP (x, 0)) == MULT)
  79. {
  80. return gen_rtx (PLUS, mode, XEXP (x, 0),
  81. plus_constant (XEXP (x, 1), c));
  82. }
  83. else if (GET_CODE (XEXP (x, 1)) == MULT)
  84. {
  85. return gen_rtx (PLUS, mode, plus_constant (XEXP (x, 0), c),
  86. XEXP (x, 1));
  87. }
  88. #endif
  89. }
  90. if (c != 0)
  91. x = gen_rtx (PLUS, mode, x, gen_rtx (CONST_INT, VOIDmode, c));
  92. if (all_constant)
  93. return gen_rtx (CONST, VOIDmode, x);
  94. else
  95. return x;
  96. }
  97. /* If X is a sum, return a new sum like X but lacking any constant terms.
  98. Add all the removed constant terms into *CONSTPTR.
  99. X itself is not altered. The result != X if and only if
  100. it is not isomorphic to X. */
  101. rtx
  102. eliminate_constant_term (x, constptr)
  103. rtx x;
  104. int *constptr;
  105. {
  106. int c;
  107. register rtx x0, x1;
  108. if (GET_CODE (x) != PLUS)
  109. return x;
  110. /* First handle constants appearing at this level explicitly. */
  111. if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  112. {
  113. *constptr += INTVAL (XEXP (x, 0));
  114. return eliminate_constant_term (XEXP (x, 1), constptr);
  115. }
  116. if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  117. {
  118. *constptr += INTVAL (XEXP (x, 1));
  119. return eliminate_constant_term (XEXP (x, 0), constptr);
  120. }
  121. c = 0;
  122. x0 = eliminate_constant_term (XEXP (x, 0), &c);
  123. x1 = eliminate_constant_term (XEXP (x, 1), &c);
  124. if (x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  125. {
  126. *constptr += c;
  127. return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  128. }
  129. return x;
  130. }
  131. /* Return an rtx for the size in bytes of the value of EXP. */
  132. rtx
  133. expr_size (exp)
  134. tree exp;
  135. {
  136. return expand_expr (size_in_bytes (TREE_TYPE (exp)), 0, SImode, 0);
  137. }
  138. /* Not yet really written since C does not need it. */
  139. rtx
  140. lookup_static_chain ()
  141. {
  142. abort ();
  143. }
  144. /* Return a copy of X in which all memory references
  145. and all constants that involve symbol refs
  146. have been replaced with new temporary registers.
  147. Also emit code to load the memory locations and constants
  148. into those registers.
  149. If X contains no such constants or memory references,
  150. X itself (not a copy) is returned.
  151. X may contain no arithmetic except addition, subtraction and multiplication.
  152. Values returned by expand_expr with 1 for sum_ok fit this constraint. */
  153. static rtx
  154. break_out_memory_refs (x)
  155. register rtx x;
  156. {
  157. if (GET_CODE (x) == MEM || GET_CODE (x) == CONST
  158. || GET_CODE (x) == SYMBOL_REF)
  159. {
  160. register rtx temp = gen_reg_rtx (Pmode);
  161. if (GET_MODE (x) != Pmode && GET_MODE (x) != VOIDmode)
  162. abort ();
  163. emit_move_insn (temp, x);
  164. /* Let optimizers know that TEMP's value never changes
  165. and that X can be substituted for it. */
  166. if (GET_CODE (x) != MEM)
  167. REG_NOTES (get_last_insn ()) = gen_rtx (EXPR_LIST, REG_CONST, temp, 0);
  168. mark_reg_pointer (temp);
  169. x = temp;
  170. }
  171. else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  172. || GET_CODE (x) == MULT)
  173. {
  174. register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  175. register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  176. if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  177. x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  178. }
  179. return x;
  180. }
  181. /* Given a memory address or facsimile X, construct a new address,
  182. currently equivalent, that is stable: future stores won't change it.
  183. X must be composed of constants, register and memory references
  184. combined with addition, subtraction and multiplication:
  185. in other words, just what you can get from expand_expr if sum_ok is 1.
  186. Works by making copies of all regs and memory locations used
  187. by X and combining them the same way X does.
  188. You could also stabilize the reference to this address
  189. by copying the address to a register with copy_to_reg;
  190. but then you wouldn't get indexed addressing in the reference. */
  191. rtx
  192. copy_all_regs (x)
  193. register rtx x;
  194. {
  195. if (GET_CODE (x) == MEM || GET_CODE (x) == REG)
  196. {
  197. x = copy_to_reg (x);
  198. }
  199. else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  200. || GET_CODE (x) == MULT)
  201. {
  202. register rtx op0 = copy_all_regs (XEXP (x, 0));
  203. register rtx op1 = copy_all_regs (XEXP (x, 1));
  204. if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  205. x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  206. }
  207. return x;
  208. }
  209. /* Return something equivalent to X but valid as a memory address
  210. for something of mode MODE. When X is not itself valid, this
  211. works by copying X or subexpressions of it into registers. */
  212. rtx
  213. memory_address (mode, x)
  214. enum machine_mode mode;
  215. register rtx x;
  216. {
  217. register rtx tem, oldx;
  218. /* By passing constant addresses thru registers
  219. we get a chance to cse them. */
  220. if (! cse_not_expected && CONSTANT_ADDRESS_P (x))
  221. {
  222. tem = copy_to_suggested_reg (x, gen_reg_rtx (Pmode));
  223. REG_NOTES (get_last_insn ()) = gen_rtx (EXPR_LIST, REG_CONST, tem, 0);
  224. return tem;
  225. }
  226. /* Accept a QUEUED that refers to a REG
  227. even though that isn't a valid address.
  228. On attempting to put this in an insn we will call protect_from_queue
  229. which will turn it into a REG, which is valid. */
  230. if (GET_CODE (x) == QUEUED
  231. && GET_CODE (QUEUED_VAR (x)) == REG)
  232. return x;
  233. /* We get better cse by rejecting indirect addressing at this stage.
  234. Let the combiner create indirect addresses where appropriate.
  235. For now, generate the code so that the subexpressions useful to share
  236. are visible. But not if cse won't be done! */
  237. oldx = x;
  238. if (! cse_not_expected && GET_CODE (x) != REG)
  239. x = break_out_memory_refs (x);
  240. /* At this point, any valid address is accepted. */
  241. GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  242. /* If it was valid before but breaking out memory refs invalidated it,
  243. use it the old way. */
  244. GO_IF_LEGITIMATE_ADDRESS (mode, oldx, win2);
  245. /* Perform machine-dependent transformations on X
  246. in certain cases. This is not necessary since the code
  247. below can handle all possible cases, but machine-dependent
  248. transformations can make better code. */
  249. LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  250. /* PLUS and MULT can appear in special ways
  251. as the result of attempts to make an address usable for indexing.
  252. Usually they are dealt with by calling force_operand, below.
  253. But a sum containing constant terms is special
  254. if removing them makes the sum a valid address:
  255. then we generate that address in a register
  256. and index off of it. We do this because it often makes
  257. shorter code, and because the addresses thus generated
  258. in registers often become common subexpressions. */
  259. if (GET_CODE (x) == PLUS)
  260. {
  261. int constant_term = 0;
  262. rtx y = eliminate_constant_term (x, &constant_term);
  263. if (constant_term != 0)
  264. GO_IF_LEGITIMATE_ADDRESS (mode, y, win1);
  265. return force_operand (x, 0);
  266. win1:
  267. return plus_constant (copy_to_reg (y), constant_term);
  268. }
  269. if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  270. return force_operand (x, 0);
  271. /* Last resort: copy the value to a register, since
  272. the register is a valid address. */
  273. return copy_to_reg (x);
  274. win2:
  275. x = oldx;
  276. win:
  277. if (force_addr && GET_CODE (x) != REG)
  278. return copy_to_reg (x);
  279. return x;
  280. }
  281. /* Return 1 if X and Y are identical-looking rtx's.
  282. This is the Lisp function EQUAL for rtx arguments. */
  283. int
  284. rtx_equal_p (x, y)
  285. rtx x, y;
  286. {
  287. register int i;
  288. register int hash = 0;
  289. register RTX_CODE code = GET_CODE (x);
  290. register char *fmt;
  291. if (x == y)
  292. return 1;
  293. /* Rtx's of different codes cannot be equal. */
  294. if (code != GET_CODE (y))
  295. return 0;
  296. /* These three types of rtx's can be compared nonrecursively. */
  297. if (code == REG)
  298. return (REGNO (x) == REGNO (y));
  299. if (code == LABEL_REF)
  300. return XEXP (x, 0) == XEXP (y, 0);
  301. if (code == SYMBOL_REF)
  302. return XSTR (x, 0) == XSTR (y, 0);
  303. /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
  304. if (GET_MODE (x) != GET_MODE (y))
  305. return 0;
  306. /* Compare the elements. If any pair of corresponding elements
  307. fail to match, return 0 for the whole things. */
  308. fmt = GET_RTX_FORMAT (code);
  309. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  310. {
  311. switch (fmt[i])
  312. {
  313. case 'i':
  314. if (XINT (x, i) != XINT (y, i))
  315. return 0;
  316. break;
  317. case 'e':
  318. if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
  319. return 0;
  320. break;
  321. case '0':
  322. break;
  323. /* It is believed that rtx's at this level will never
  324. contain anything but integers and other rtx's,
  325. except for within LABEL_REFs and SYMBOL_REFs. */
  326. default:
  327. abort ();
  328. }
  329. }
  330. return 1;
  331. }
  332. /* Return a modified copy of X with its memory address copied
  333. into a temporary register to protect it from side effects.
  334. If X is not a MEM, it is returned unchanged (and not copied).
  335. Perhaps even if it is a MEM, if there is no need to change it. */
  336. rtx
  337. stabilize (x)
  338. rtx x;
  339. {
  340. register rtx addr;
  341. if (GET_CODE (x) != MEM)
  342. return x;
  343. addr = XEXP (x, 0);
  344. if (rtx_varies_p (addr))
  345. {
  346. rtx temp = copy_all_regs (addr);
  347. rtx mem;
  348. if (GET_CODE (temp) != REG)
  349. temp = copy_to_reg (temp);
  350. mem = gen_rtx (MEM, GET_MODE (x), temp);
  351. /* Mark returned memref with in_struct
  352. if it's in an array or structure. */
  353. if (GET_CODE (addr) == PLUS || x->in_struct)
  354. mem->in_struct = 1;
  355. return mem;
  356. }
  357. return x;
  358. }
  359. /* Copy the value or contents of X to a new temp reg and return that reg. */
  360. rtx
  361. copy_to_reg (x)
  362. rtx x;
  363. {
  364. register rtx temp = gen_reg_rtx (GET_MODE (x));
  365. emit_move_insn (temp, x);
  366. return temp;
  367. }
  368. /* If X is a memory ref, copy its contents to a new temp reg and return
  369. that reg. Otherwise, return X. */
  370. rtx
  371. force_not_mem (x)
  372. rtx x;
  373. {
  374. register rtx temp;
  375. if (GET_CODE (x) != MEM)
  376. return x;
  377. temp = gen_reg_rtx (GET_MODE (x));
  378. emit_move_insn (temp, x);
  379. return temp;
  380. }
  381. /* Copy X to TARGET (if it's nonzero and a reg)
  382. or to a new temp reg and return that reg. */
  383. rtx
  384. copy_to_suggested_reg (x, target)
  385. rtx x, target;
  386. {
  387. register rtx temp;
  388. if (target && GET_CODE (target) == REG)
  389. temp = target;
  390. else
  391. temp = gen_reg_rtx (GET_MODE (x));
  392. emit_move_insn (temp, x);
  393. return temp;
  394. }
  395. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  396. This pops when ADJUST is positive. ADJUST need not be constant. */
  397. void
  398. adjust_stack (adjust)
  399. rtx adjust;
  400. {
  401. adjust = protect_from_queue (adjust, 0);
  402. #ifdef STACK_GROWS_DOWNWARD
  403. emit_insn (gen_add2_insn (gen_rtx (REG, SImode, STACK_POINTER_REGNUM),
  404. adjust));
  405. #else
  406. emit_insn (gen_sub2_insn (gen_rtx (REG, SImode, STACK_POINTER_REGNUM),
  407. adjust));
  408. #endif
  409. }
  410. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  411. This pushes when ADJUST is positive. ADJUST need not be constant. */
  412. void
  413. anti_adjust_stack (adjust)
  414. rtx adjust;
  415. {
  416. adjust = protect_from_queue (adjust, 0);
  417. #ifdef STACK_GROWS_DOWNWARD
  418. emit_insn (gen_sub2_insn (gen_rtx (REG, SImode, STACK_POINTER_REGNUM),
  419. adjust));
  420. #else
  421. emit_insn (gen_add2_insn (gen_rtx (REG, SImode, STACK_POINTER_REGNUM),
  422. adjust));
  423. #endif
  424. }
  425. /* Return a pseudo reg representing the value returned by
  426. a function call that was just emitted. */
  427. rtx
  428. function_value (mode)
  429. enum machine_mode mode;
  430. {
  431. /* Copy register 0 in case the value we return
  432. will not get used until after another function call happens. */
  433. return copy_to_reg (gen_rtx (REG, mode, FUNCTION_VALUE_REGNUM));
  434. }
  435. /* Return a hard reg representing the value returned by
  436. a function call that was just emitted. */
  437. rtx
  438. hard_function_value (mode)
  439. enum machine_mode mode;
  440. {
  441. return gen_rtx (REG, mode, FUNCTION_VALUE_REGNUM);
  442. }
  443. /* Return a pseudo reg representing the value returned by
  444. a function call that was just emitted. */
  445. void
  446. copy_function_value (reg)
  447. rtx reg;
  448. {
  449. emit_move_insn (reg, gen_rtx (REG, GET_MODE (reg), FUNCTION_VALUE_REGNUM));
  450. }