expmed.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /* Medium-level subroutines: convert bit-field store and extract
  2. and shifts, multiplies and divides to rtl instructions.
  3. Copyright (C) 1987 Free Software Foundation, Inc.
  4. This file is part of GNU CC.
  5. GNU CC is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY. No author or distributor
  7. accepts responsibility to anyone for the consequences of using it
  8. or for whether it serves any particular purpose or works at all,
  9. unless he says so in writing. Refer to the GNU CC General Public
  10. License for full details.
  11. Everyone is granted permission to copy, modify and redistribute
  12. GNU CC, but only under the conditions described in the
  13. GNU CC General Public License. A copy of this license is
  14. supposed to have been given to you along with GNU CC so you
  15. can know your rights and responsibilities. It should be in a
  16. file named COPYING. Among other things, the copyright notice
  17. and this notice must be preserved on all copies. */
  18. #include "config.h"
  19. #include "rtl.h"
  20. #include "tree.h"
  21. #include "insn-flags.h"
  22. #include "insn-codes.h"
  23. #include "expr.h"
  24. /* Return an rtx representing minus the value of X. */
  25. rtx
  26. negate_rtx (x)
  27. rtx x;
  28. {
  29. if (GET_CODE (x) == CONST_INT)
  30. return gen_rtx (CONST_INT, VOIDmode, - INTVAL (x));
  31. else
  32. return expand_binop (sub_optab, GET_MODE (x), const0_rtx, x, 0,
  33. 0, OPTAB_LIB_WIDEN);
  34. }
  35. /* Generate code to store value from rtx VALUE
  36. into a bit-field within structure STR_RTX as specified by FIELD. */
  37. rtx
  38. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value)
  39. rtx str_rtx;
  40. register int bitsize;
  41. int bitnum;
  42. enum machine_mode fieldmode;
  43. rtx value;
  44. {
  45. int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  46. register int offset = bitnum / unit;
  47. register int bitpos = bitnum % unit;
  48. register rtx op0 = str_rtx;
  49. rtx value1;
  50. if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD)
  51. {
  52. /* Storing in a full-word or multi-word field in a register
  53. can be done with just SUBREG. */
  54. emit_move_insn (gen_rtx (SUBREG, fieldmode, op0, offset),
  55. value);
  56. return value;
  57. }
  58. /* Storing an lsb-aligned field in a register
  59. can be done with a movestrict instruction. */
  60. if (GET_CODE (str_rtx) == REG
  61. #ifdef BYTES_BIG_ENDIAN
  62. && bitpos + bitsize == unit
  63. #else
  64. && bitpos == 0
  65. #endif
  66. && movstrict_optab[(int) fieldmode].insn_code != CODE_FOR_nothing)
  67. {
  68. /* Get appropriate low part of the value being stored. */
  69. if (!(GET_CODE (value) == SYMBOL_REF
  70. || GET_CODE (value) == LABEL_REF
  71. || GET_CODE (value) == CONST))
  72. value = gen_lowpart (fieldmode, value);
  73. emit_insn (GEN_FCN (movstrict_optab[(int) fieldmode].insn_code)
  74. (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  75. return value;
  76. }
  77. /* From here on we can assume that the field to be stored in is an integer,
  78. since it is shorter than a word. */
  79. if (GET_CODE (str_rtx) != REG)
  80. op0 = protect_from_queue (str_rtx, 1);
  81. value = protect_from_queue (value, 0);
  82. /* Add OFFSET into OP0's address. Also,
  83. if op0 is a register, we need it in SImode. */
  84. if (GET_CODE (op0) == SUBREG)
  85. {
  86. offset += SUBREG_WORD (op0);
  87. op0 = SUBREG_REG (op0);
  88. }
  89. if (GET_CODE (op0) == REG)
  90. {
  91. if (offset != 0 || GET_MODE (op0) != SImode)
  92. op0 = gen_rtx (SUBREG, SImode, op0, offset);
  93. offset = 0;
  94. }
  95. /* Now OFFSET is nonzero only if OP0 is memory
  96. and is therefore always measured in bytes. */
  97. #ifdef HAVE_insv
  98. if (HAVE_insv
  99. && !(bitsize == 1 && GET_CODE (value) == CONST_INT))
  100. {
  101. /* Add OFFSET into OP0's address. */
  102. if (GET_CODE (op0) == MEM)
  103. op0 = gen_rtx (MEM, QImode,
  104. memory_address (QImode, plus_constant (XEXP (op0, 0),
  105. offset)));
  106. /* Convert VALUE to SImode (which insv insn wants) in VALUE1. */
  107. value1 = value;
  108. if (GET_MODE (value) != SImode)
  109. {
  110. if (GET_CODE (value) == REG
  111. && GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  112. /* Optimization: Don't bother really extending VALUE
  113. if it has all the bits we will actually use. */
  114. value1 = gen_rtx (SUBREG, SImode, value, 0);
  115. else if (!CONSTANT_ADDRESS_P (value))
  116. /* Parse phase is supposed to make VALUE's data type
  117. match that of the component reference, which is a type
  118. at least as wide as the field; so VALUE should have
  119. a mode that corresponds to that type. */
  120. abort ();
  121. }
  122. /* On big-endian machines, we count bits from the most significant.
  123. If the bit field insn does not, we must invert. */
  124. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  125. bitpos = unit - 1 - bitpos;
  126. #endif
  127. emit_insn (gen_insv (op0,
  128. gen_rtx (CONST_INT, VOIDmode, bitsize),
  129. gen_rtx (CONST_INT, VOIDmode, bitpos),
  130. value1));
  131. }
  132. else
  133. #endif
  134. /* Insv is not available; store using shifts and boolean ops. */
  135. store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
  136. return value;
  137. }
  138. /* Use shifts and boolean operations to store VALUE
  139. into a bit field of width BITSIZE
  140. in a memory location specified by OP0 except offset by OFFSET bytes.
  141. The field starts at position BITPOS within the byte.
  142. (If OP0 is a register, it is SImode, and BITPOS is the starting
  143. position within the word.)
  144. Note that protect_from_queue has already been done on OP0 and VALUE. */
  145. rtx
  146. store_fixed_bit_field (op0, offset, bitsize, bitpos, value)
  147. register rtx op0;
  148. register int offset, bitsize, bitpos;
  149. register rtx value;
  150. {
  151. register enum machine_mode mode;
  152. int total_bits = BITS_PER_WORD;
  153. rtx subtarget;
  154. int all_zero = 0;
  155. int all_one = 0;
  156. /* Add OFFSET to OP0's address (if it is in memory)
  157. and if a single byte contains the whole bit field
  158. change OP0 to a byte. */
  159. if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  160. ; /* OFFSET always 0 for register */
  161. else if (bitsize + bitpos <= BITS_PER_UNIT)
  162. {
  163. total_bits = BITS_PER_UNIT;
  164. op0 = gen_rtx (MEM, QImode,
  165. memory_address (QImode,
  166. plus_constant (XEXP (op0, 0), offset)));
  167. }
  168. else
  169. {
  170. /* Get ref to word containing the field. */
  171. /* Adjust BITPOS to be position within a word,
  172. and OFFSET to be the offset of that word.
  173. Then alter OP0 to refer to that word. */
  174. bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  175. offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  176. op0 = gen_rtx (MEM, SImode,
  177. memory_address (SImode,
  178. plus_constant (XEXP (op0, 0), offset)));
  179. }
  180. mode = GET_MODE (op0);
  181. /* Now OP0 is either a byte or a word, and the bit field is contained
  182. entirely within it. TOTAL_BITS and MODE say which one (byte or word).
  183. BITPOS is the starting bit number within the byte or word. */
  184. #ifdef BYTES_BIG_ENDIAN
  185. /* BITPOS is the distance between our msb
  186. and that of the containing byte or word.
  187. Convert it to the distance from the lsb. */
  188. bitpos = total_bits - bitsize - bitpos;
  189. #endif
  190. /* Now BITPOS is always the distance between our lsb
  191. and that of the containing byte or word. */
  192. /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
  193. we must first convert its mode to MODE. */
  194. if (GET_CODE (value) == CONST_INT)
  195. {
  196. register int v = INTVAL (value);
  197. if (v == 0)
  198. all_zero = 1;
  199. else if (v == (1 << bitsize) - 1)
  200. all_one = 1;
  201. value = gen_rtx (CONST_INT, VOIDmode, v << bitpos);
  202. }
  203. else
  204. {
  205. if (GET_MODE (value) != mode)
  206. {
  207. if (GET_CODE (value) == REG && mode == QImode)
  208. value = gen_rtx (SUBREG, mode, value, 0);
  209. else
  210. {
  211. rtx temp = gen_reg_rtx (mode);
  212. convert_move (temp, value, 1);
  213. value = temp;
  214. }
  215. }
  216. value = expand_bit_and (mode, value,
  217. gen_rtx (CONST_INT, VOIDmode,
  218. (1 << bitsize) - 1),
  219. 0);
  220. if (bitpos > 0)
  221. value = expand_shift (LSHIFT_EXPR, mode, value,
  222. build_int_2 (bitpos, 0), 0, 1);
  223. }
  224. /* Now clear the chosen bits in OP0,
  225. except that if VALUE is -1 we need not bother. */
  226. subtarget = op0;
  227. if (! all_one)
  228. subtarget = expand_bit_and (GET_MODE (op0), op0,
  229. gen_rtx (CONST_INT, VOIDmode,
  230. ~ (((1 << bitsize) - 1) << bitpos)),
  231. subtarget);
  232. /* Now logical-or VALUE into OP0, unless it is zero. */
  233. if (! all_zero)
  234. subtarget = expand_binop (mode, ior_optab, subtarget, value, op0,
  235. 1, OPTAB_LIB_WIDEN);
  236. if (op0 != subtarget)
  237. emit_move_insn (op0, subtarget);
  238. return op0;
  239. }
  240. /* Generate code to extract a byte-field as specified by STR_RTX and FIELD
  241. and put it in TARGET (if TARGET is nonzero). Regardless of TARGET,
  242. we return the rtx for where the value is placed. It may be a QUEUED.
  243. STR_RTX is the structure containing the byte (a REG or MEM).
  244. FIELD is the tree, a FIELD_DECL, describing the field.
  245. MODE is the natural mode of the field; BImode for
  246. fields that are not integral numbers of bytes.
  247. TMODE is the mode the caller would like the value to have;
  248. but the value may be returned with type MODE instead.
  249. If a TARGET is specified and we can store in it at no extra cost,
  250. we do so, and return TARGET.
  251. Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  252. if they are equally easy. */
  253. rtx
  254. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp, target, mode, tmode)
  255. rtx str_rtx;
  256. register int bitsize;
  257. int bitnum;
  258. int unsignedp;
  259. rtx target;
  260. enum machine_mode mode, tmode;
  261. {
  262. int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  263. register int offset = bitnum / unit;
  264. register int bitpos = bitnum % unit;
  265. register rtx op0 = str_rtx;
  266. rtx spec_target = target;
  267. rtx bitsize_rtx, bitpos_rtx;
  268. rtx spec_target_subreg = 0;
  269. if (tmode == VOIDmode)
  270. tmode = mode;
  271. /* Extracting a full-word or multi-word value
  272. from a structure in a register.
  273. This can be done with just SUBREG.
  274. So too extracting a subword value in
  275. the least significant part of the register. */
  276. if (bitsize >= BITS_PER_WORD
  277. || (bitsize == GET_MODE_BITSIZE (mode)
  278. #ifdef BYTES_BIG_ENDIAN
  279. && bitpos + bitsize == BITS_PER_WORD
  280. #else
  281. && bitpos == 0
  282. #endif
  283. ))
  284. {
  285. /* MODE is used here, not TMODE, because MODE specifies
  286. how much of the register we want to extract.
  287. We could convert the value to TMODE afterward,
  288. but no need to bother, since the caller will do it. */
  289. if (mode == GET_MODE (op0))
  290. return op0;
  291. return gen_rtx (SUBREG, mode, op0, offset);
  292. }
  293. /* From here on we know the desired field is smaller than a word
  294. so we can assume it is an integer. So we can safely extract it as one
  295. size of integer, if necessary, and then truncate or extend
  296. to the size that is wanted. */
  297. /* OFFSET is the number of words or bytes (UNIT says which)
  298. from STR_RTX to the first word or byte containing part of the field. */
  299. if (GET_CODE (str_rtx) == REG)
  300. {
  301. if (offset != 0)
  302. op0 = gen_rtx (SUBREG, SImode, op0, offset);
  303. }
  304. else
  305. {
  306. op0 = protect_from_queue (str_rtx, 1);
  307. }
  308. /* Now OFFSET is nonzero only for memory operands. */
  309. if (unsignedp)
  310. {
  311. #ifdef HAVE_extzv
  312. if (HAVE_extzv)
  313. {
  314. /* Get ref to first byte containing part of the field. */
  315. if (GET_CODE (str_rtx) != REG)
  316. op0 = gen_rtx (MEM, QImode,
  317. memory_address (QImode,
  318. plus_constant (XEXP (op0, 0), offset)));
  319. /* If op0 is a register, we need it in SImode
  320. to make it acceptable to the format of extv. */
  321. if (GET_CODE (op0) == SUBREG)
  322. PUT_MODE (op0, SImode);
  323. if (GET_CODE (op0) == REG)
  324. op0 = gen_rtx (SUBREG, SImode, op0, 0);
  325. if (target == 0)
  326. target = spec_target = gen_reg_rtx (tmode);
  327. if (GET_MODE (target) != SImode)
  328. {
  329. if (GET_CODE (target) == REG)
  330. spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  331. else
  332. target = gen_reg_rtx (SImode);
  333. }
  334. /* On big-endian machines, we count bits from the most significant.
  335. If the bit field insn does not, we must invert. */
  336. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  337. bitpos = unit - 1 - bitpos;
  338. #endif
  339. bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  340. bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  341. emit_insn (gen_extzv (protect_from_queue (target, 1),
  342. op0, bitsize_rtx, bitpos_rtx));
  343. }
  344. else
  345. #endif
  346. target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  347. target, 1);
  348. }
  349. else
  350. {
  351. #ifdef HAVE_extv
  352. if (HAVE_extv)
  353. {
  354. /* Get ref to first byte containing part of the field. */
  355. if (GET_CODE (str_rtx) != REG)
  356. op0 = gen_rtx (MEM, QImode,
  357. memory_address (QImode,
  358. plus_constant (XEXP (op0, 0), offset)));
  359. /* If op0 is a register, we need it in QImode
  360. to make it acceptable to the format of extv. */
  361. if (GET_CODE (op0) == SUBREG)
  362. PUT_MODE (op0, SImode);
  363. if (GET_CODE (op0) == REG)
  364. op0 = gen_rtx (SUBREG, SImode, op0, 0);
  365. if (target == 0)
  366. target = spec_target = gen_reg_rtx (tmode);
  367. if (GET_MODE (target) != SImode)
  368. {
  369. if (GET_CODE (target) == REG)
  370. spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  371. else
  372. target = gen_reg_rtx (SImode);
  373. }
  374. /* On big-endian machines, we count bits from the most significant.
  375. If the bit field insn does not, we must invert. */
  376. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  377. bitpos = unit - 1 - bitpos;
  378. #endif
  379. bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  380. bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  381. emit_insn (gen_extv (protect_from_queue (target, 1), op0,
  382. bitsize_rtx, bitpos_rtx));
  383. }
  384. else
  385. #endif
  386. target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  387. target, 0);
  388. }
  389. if (target == spec_target)
  390. return target;
  391. if (target == spec_target_subreg)
  392. return spec_target;
  393. if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  394. return convert_to_mode (tmode, target);
  395. return target;
  396. }
  397. /* Extract a bit field using shifts and boolean operations
  398. Returns an rtx to represent the value.
  399. OP0 addresses a register (word) or memory (byte).
  400. BITPOS says which bit within the word or byte the bit field starts in.
  401. OFFSET says how many bytes farther the bit field starts;
  402. it is 0 if OP0 is a register.
  403. BITSIZE says how many bits long the bit field is.
  404. UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  405. If TARGET is nonzero, attempts to store the value there
  406. and return TARGET, but this is not guaranteed.
  407. If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
  408. rtx
  409. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos, target, unsignedp)
  410. enum machine_mode tmode;
  411. register rtx op0, target;
  412. register int offset, bitsize, bitpos;
  413. int unsignedp;
  414. {
  415. int total_bits = BITS_PER_WORD;
  416. enum machine_mode mode;
  417. /* If the bit field fits entirely in one byte of memory,
  418. let OP0 be that byte. We must add OFFSET to its address. */
  419. if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  420. ; /* OFFSET is 0 for registers */
  421. else if (bitsize + bitpos <= BITS_PER_UNIT)
  422. {
  423. total_bits = BITS_PER_UNIT;
  424. op0 = gen_rtx (MEM, QImode,
  425. memory_address (QImode,
  426. plus_constant (XEXP (op0, 0), offset)));
  427. }
  428. else
  429. {
  430. /* Get ref to word containing the field. */
  431. /* Adjust BITPOS to be position within a word,
  432. and OFFSET to be the offset of that word. */
  433. bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  434. offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  435. op0 = gen_rtx (MEM, SImode,
  436. memory_address (SImode,
  437. plus_constant (XEXP (op0, 0), offset)));
  438. }
  439. mode = GET_MODE (op0);
  440. #ifdef BYTES_BIG_ENDIAN
  441. /* On big-endian machines, we count bits from the most significant. */
  442. if (unsignedp)
  443. {
  444. if (bitsize + bitpos != total_bits)
  445. {
  446. /* If the field is not already ending at the lsb,
  447. shift it so it does. */
  448. tree amount = build_int_2 (total_bits - (bitsize + bitpos), 0);
  449. /* Maybe propagate the target for the shift. */
  450. /* Certainly do so if we will return the value of the shift. */
  451. rtx subtarget = ((bitpos == 0
  452. || (target != 0 && GET_CODE (target) == REG))
  453. ? target : 0);
  454. if (tmode != mode) subtarget = 0;
  455. op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  456. }
  457. /* Unless the msb of the field used to be the msb of the word,
  458. mask out the upper bits. If we want the value in a wider mode
  459. than we have now, let this bit_and clear the high bits. */
  460. if (mode != tmode && GET_CODE (op0) == REG)
  461. op0 = gen_rtx (SUBREG, tmode, op0, 0);
  462. else if (mode != tmode)
  463. target = 0;
  464. if (bitpos != 0)
  465. return expand_bit_and (GET_MODE (op0), op0,
  466. gen_rtx (CONST_INT, VOIDmode, (1 << bitsize) - 1),
  467. target);
  468. return op0;
  469. }
  470. /* To extract a signed bit-field, first shift its msb to the msb of the word,
  471. then arithmetic-shift its lsb to the lsb of the word. */
  472. if (mode != tmode)
  473. target = 0;
  474. if (bitpos != 0)
  475. {
  476. tree amount = build_int_2 (bitpos, 0);
  477. /* Maybe propagate the target for the shift. */
  478. /* Certainly do so if we will return the value of the shift. */
  479. rtx subtarget = (target != 0 && GET_CODE (target) == REG
  480. ? target : 0);
  481. op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  482. }
  483. return expand_shift (LSHIFT_EXPR, mode, op0,
  484. build_int_2 (total_bits - bitsize, 0),
  485. target, 1);
  486. #else /* not BYTES_BIG_ENDIAN */
  487. abort (); /* Not written yet since Vax has extv. */
  488. #endif /* not BYTES_BIG_ENDIAN */
  489. }
  490. /* Output a shift instruction for expression code CODE,
  491. with SHIFTED being the rtx for the value to shift,
  492. and AMOUNT the tree for the amount to shift by.
  493. Store the result in the rtx TARGET, if that is convenient.
  494. If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  495. Return the rtx for where the value is. */
  496. /* Pastel, for shifts, converts shift count to SImode here
  497. independent of the mode being shifted.
  498. Should that be done in an earlier pass?
  499. It turns out not to matter for C. */
  500. rtx
  501. expand_shift (code, mode, shifted, amount, target, unsignedp)
  502. enum tree_code code;
  503. register enum machine_mode mode;
  504. rtx shifted;
  505. tree amount;
  506. register rtx target;
  507. int unsignedp;
  508. {
  509. register rtx op1, temp = 0;
  510. register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  511. int try;
  512. rtx negated = 0;
  513. /* Previously detected shift-counts computed by NEGATE_EXPR
  514. and shifted in the other direction; but that does not work
  515. on all machines. */
  516. op1 = expand_expr (amount, 0, VOIDmode, 0);
  517. for (try = 0; temp == 0 && try < 3; try++)
  518. {
  519. enum optab_methods methods;
  520. if (try == 0)
  521. methods = OPTAB_DIRECT;
  522. else if (try == 1)
  523. methods = OPTAB_WIDEN;
  524. else
  525. methods = OPTAB_LIB_WIDEN;
  526. if (code == LROTATE_EXPR || code == RROTATE_EXPR)
  527. {
  528. /* Widening does not work for rotation. */
  529. if (methods != OPTAB_DIRECT)
  530. methods = OPTAB_LIB;
  531. temp = expand_binop (mode,
  532. left ? rotl_optab : rotr_optab,
  533. shifted, op1, target, -1, methods);
  534. /* If there is no shift instruction for the desired direction,
  535. try negating the shift count and shifting in the other direction.
  536. If a machine has only a left shift instruction
  537. then we are entitled to assume it shifts right with negative args. */
  538. if (temp == 0)
  539. {
  540. if (negated != 0)
  541. ;
  542. else if (GET_CODE (op1) == CONST_INT)
  543. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  544. else
  545. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  546. temp = expand_binop (mode,
  547. left ? rotr_optab : rotl_optab,
  548. shifted, negated, target, -1, methods);
  549. }
  550. }
  551. else if (unsignedp)
  552. {
  553. temp = expand_binop (mode,
  554. left ? lshl_optab : lshr_optab,
  555. shifted, op1, target, unsignedp, methods);
  556. if (temp == 0 && left)
  557. temp = expand_binop (mode, ashl_optab,
  558. shifted, op1, target, unsignedp, methods);
  559. if (temp == 0)
  560. {
  561. if (negated != 0)
  562. ;
  563. else if (GET_CODE (op1) == CONST_INT)
  564. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  565. else
  566. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  567. temp = expand_binop (mode,
  568. left ? lshr_optab : lshl_optab,
  569. shifted, negated,
  570. target, unsignedp, methods);
  571. }
  572. if (temp != 0)
  573. return temp;
  574. /* No logical shift insn in either direction =>
  575. try to do it with a bit-field extract instruction if we have one. */
  576. #ifdef HAVE_extzv
  577. if (HAVE_extzv && GET_CODE (op1) == CONST_INT
  578. && methods == OPTAB_DIRECT
  579. && mode == SImode && (INTVAL (op1) < 0) == left)
  580. {
  581. if (target == 0) target = gen_reg_rtx (mode);
  582. if (left)
  583. op1 = gen_rtx (CONST_INT, VOIDmode, - INTVAL (op1));
  584. emit_insn (gen_extzv (protect_from_queue (target, 1),
  585. protect_from_queue (shifted, 0),
  586. gen_rtx (CONST_INT,
  587. VOIDmode,
  588. (mode_size[(int) mode]
  589. * BITS_PER_UNIT)
  590. - INTVAL (op1)),
  591. protect_from_queue (op1, 1)));
  592. return target;
  593. }
  594. /* Can also do logical shift with signed bit-field extract
  595. followed by inserting the bit-field at a different position. */
  596. #endif /* HAVE_extzv */
  597. /* We have failed to generate the logical shift and will abort. */
  598. }
  599. else
  600. {
  601. /* Arithmetic shift */
  602. temp = expand_binop (mode,
  603. left ? ashl_optab : ashr_optab,
  604. shifted, op1, target, unsignedp, methods);
  605. if (temp == 0)
  606. {
  607. if (negated != 0)
  608. ;
  609. else if (GET_CODE (op1) == CONST_INT)
  610. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  611. else
  612. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  613. temp = expand_binop (mode,
  614. left ? ashr_optab : ashl_optab,
  615. shifted, negated, target, unsignedp, methods);
  616. }
  617. }
  618. }
  619. if (temp == 0)
  620. abort ();
  621. return temp;
  622. }
  623. /* Output an instruction or two to bitwise-and OP0 with OP1
  624. in mode MODE, with output to TARGET if convenient and TARGET is not zero.
  625. Returns where the result is. */
  626. rtx
  627. expand_bit_and (mode, op0, op1, target)
  628. enum machine_mode mode;
  629. rtx op0, op1, target;
  630. {
  631. register rtx temp;
  632. /* First try to open-code it directly. */
  633. temp = expand_binop (mode, and_optab, op0, op1, target, 1, OPTAB_DIRECT);
  634. if (temp == 0)
  635. {
  636. /* If that fails, try to open code using a clear-bits insn. */
  637. if (GET_CODE (op1) == CONST_INT)
  638. op1 = gen_rtx (CONST_INT, VOIDmode, ~ INTVAL (op1));
  639. else
  640. op1 = expand_unop (mode, one_cmpl_optab, op1, 0, 1);
  641. temp = expand_binop (mode, andcb_optab, op0, op1, target,
  642. 1, OPTAB_DIRECT);
  643. }
  644. if (temp == 0)
  645. /* If still no luck, try library call or wider modes. */
  646. temp = expand_binop (mode, and_optab, op0, op1, target,
  647. 1, OPTAB_LIB_WIDEN);
  648. if (temp == 0)
  649. abort ();
  650. return temp;
  651. }
  652. /* Perform a multiplication and return an rtx for the result.
  653. MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  654. TARGET is a suggestion for where to store the result (an rtx).
  655. We check specially for a constant integer as OP1.
  656. If you want this check for OP0 as well, then before calling
  657. you should swap the two operands if OP0 would be constant. */
  658. rtx
  659. expand_mult (mode, op0, op1, target, unsignedp)
  660. enum machine_mode mode;
  661. register rtx op0, op1, target;
  662. int unsignedp;
  663. {
  664. if (GET_CODE (op1) == CONST_INT)
  665. {
  666. register int foo = exact_log2 (INTVAL (op1));
  667. int bar;
  668. /* Is multiplier a power of 2? */
  669. if (foo >= 0)
  670. {
  671. return expand_shift (LSHIFT_EXPR, mode, op0,
  672. build_int_2 (foo, 0),
  673. target, 0);
  674. }
  675. /* Is multiplier a sum of two powers of 2? */
  676. bar = floor_log2 (INTVAL (op1));
  677. foo = exact_log2 (INTVAL (op1) - (1 << bar));
  678. if (bar >= 0 && foo >= 0)
  679. {
  680. rtx pow1 = ((foo == 0) ? op0
  681. : expand_shift (LSHIFT_EXPR, mode, op0,
  682. build_int_2 (foo, 0),
  683. 0, 0));
  684. rtx pow2 = expand_shift (LSHIFT_EXPR, mode, op0,
  685. build_int_2 (bar, 0),
  686. 0, 0);
  687. return force_operand (gen_rtx (PLUS, mode, pow1, pow2), target);
  688. }
  689. }
  690. op0 = expand_binop (mode, unsignedp ? umul_optab : smul_optab,
  691. op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  692. if (op0 == 0)
  693. abort ();
  694. return op0;
  695. }
  696. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  697. if that is convenient, and returning where the result is.
  698. You may request either the quotient or the remainder as the result;
  699. specify REM_FLAG nonzero to get the remainder.
  700. CODE is the expression code for which kind of division this is;
  701. it controls how rounding is done. MODE is the machine mode to use.
  702. UNSIGNEDP nonzero means do unsigned division. */
  703. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  704. and then correct it by or'ing in missing high bits
  705. if result of ANDI is nonzero.
  706. For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  707. This could optimize to a bfexts instruction.
  708. But C doesn't use these operations, so their optimizations are
  709. left for later. */
  710. rtx
  711. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  712. int rem_flag;
  713. enum tree_code code;
  714. enum machine_mode mode;
  715. register rtx op0, op1, target;
  716. int unsignedp;
  717. {
  718. register rtx label;
  719. register rtx temp;
  720. int log = -1;
  721. int can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  722. int mod_insn_no_good = 0;
  723. rtx adjusted_op0 = op0;
  724. if (target == 0)
  725. {
  726. target = gen_reg_rtx (mode);
  727. }
  728. if (GET_CODE (op1) == CONST_INT)
  729. log = exact_log2 (INTVAL (op1));
  730. /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  731. which is really floor-division. Otherwise we will really do a divide,
  732. and we assume that is trunc-division.
  733. We must correct the dividend by adding or subtracting something
  734. based on the divisor, in order to do the kind of rounding specified
  735. by CODE. The correction depends on what kind of rounding is actually
  736. available, and that depends on whether we will shift or divide. */
  737. switch (code)
  738. {
  739. case TRUNC_MOD_EXPR:
  740. case TRUNC_DIV_EXPR:
  741. if (log >= 0 && ! unsignedp)
  742. {
  743. label = gen_label_rtx ();
  744. /* If we must (probably) subtract to get a remainder,
  745. don't clobber the original OP0 while getting the quotient. */
  746. if (rem_flag && rtx_equal_p (op0, target))
  747. target = gen_reg_rtx (mode);
  748. if (! can_clobber_op0 || rem_flag)
  749. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  750. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  751. emit_jump_insn (gen_bge (label));
  752. emit_insn (gen_add2_insn (adjusted_op0, plus_constant (op1, -1)));
  753. emit_label (label);
  754. mod_insn_no_good = 1;
  755. }
  756. break;
  757. case FLOOR_DIV_EXPR:
  758. case FLOOR_MOD_EXPR:
  759. if (log < 0 && ! unsignedp)
  760. {
  761. label = gen_label_rtx ();
  762. if (rem_flag && rtx_equal_p (op0, target))
  763. target = gen_reg_rtx (mode);
  764. if (! can_clobber_op0 || rem_flag)
  765. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  766. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  767. emit_jump_insn (gen_bge (label));
  768. emit_insn (gen_sub2_insn (adjusted_op0, op1));
  769. emit_insn (gen_add2_insn (adjusted_op0, const1_rtx));
  770. emit_label (label);
  771. mod_insn_no_good = 1;
  772. }
  773. break;
  774. case CEIL_DIV_EXPR:
  775. case CEIL_MOD_EXPR:
  776. if (rem_flag && rtx_equal_p (op0, target))
  777. target = gen_reg_rtx (mode);
  778. if (! can_clobber_op0 || rem_flag)
  779. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  780. if (log < 0)
  781. {
  782. if (! unsignedp)
  783. {
  784. label = gen_label_rtx ();
  785. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  786. emit_jump_insn (gen_ble (label));
  787. }
  788. emit_insn (gen_add2_insn (adjusted_op0, op1));
  789. emit_insn (gen_sub2_insn (adjusted_op0, const1_rtx));
  790. if (! unsignedp)
  791. emit_label (label);
  792. }
  793. else
  794. {
  795. emit_insn (gen_add2_insn (adjusted_op0, plus_constant (op1, -1)));
  796. }
  797. mod_insn_no_good = 1;
  798. break;
  799. case ROUND_DIV_EXPR:
  800. case ROUND_MOD_EXPR:
  801. if (rem_flag && rtx_equal_p (op0, target))
  802. target = gen_reg_rtx (mode);
  803. if (! can_clobber_op0 || rem_flag)
  804. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  805. if (log < 0)
  806. {
  807. op1 = expand_shift (RSHIFT_EXPR, mode, op1, integer_one_node, 0, 0);
  808. if (! unsignedp)
  809. {
  810. label = gen_label_rtx ();
  811. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  812. emit_jump_insn (gen_bge (label));
  813. expand_unop (mode, neg_optab, op1, op1, 0);
  814. emit_label (label);
  815. }
  816. emit_insn (gen_add2_insn (adjusted_op0, op1));
  817. }
  818. else
  819. {
  820. op1 = gen_rtx (CONST_INT, VOIDmode, INTVAL (op1) / 2);
  821. emit_insn (gen_add2_insn (adjusted_op0, op1));
  822. }
  823. mod_insn_no_good = 1;
  824. break;
  825. }
  826. if (rem_flag && !mod_insn_no_good)
  827. {
  828. /* Try to produce the remainder directly */
  829. if (log >= 0)
  830. {
  831. return expand_bit_and (mode, adjusted_op0,
  832. gen_rtx (CONST_INT, VOIDmode,
  833. INTVAL (op1) - 1),
  834. target);
  835. }
  836. else
  837. {
  838. /* See if we can do remainder without a library call. */
  839. temp = expand_binop (mode,
  840. unsignedp ? umod_optab : smod_optab,
  841. adjusted_op0, op1, target,
  842. unsignedp, OPTAB_WIDEN);
  843. if (temp != 0)
  844. return temp;
  845. /* No luck there.
  846. Can we do remainder and divide at once without a library call? */
  847. temp = gen_reg_rtx (mode);
  848. if (expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  849. adjusted_op0, op1, 0, temp, unsignedp))
  850. return temp;
  851. temp = 0;
  852. }
  853. }
  854. /* If we must (probably) subtract to get a remainder,
  855. make sure we don't clobber the original OP0 getting the quotient. */
  856. if (rem_flag && rtx_equal_p (op0, target))
  857. target = gen_reg_rtx (mode);
  858. /* Produce the quotient. */
  859. if (log >= 0)
  860. temp = expand_shift (RSHIFT_EXPR, mode, adjusted_op0,
  861. build_int_2 (exact_log2 (INTVAL (op1)), 0),
  862. target, unsignedp);
  863. else if (rem_flag && !mod_insn_no_good)
  864. /* If producing quotient in order to subtract for remainder,
  865. and a remainder subroutine would be ok,
  866. don't use a divide subroutine. */
  867. temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  868. adjusted_op0, op1, target, unsignedp, OPTAB_WIDEN);
  869. else
  870. temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  871. adjusted_op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  872. /* If we really want the remainder, get it by subtraction. */
  873. if (rem_flag)
  874. {
  875. if (temp == 0)
  876. {
  877. /* No divide instruction either. Use library for remainder. */
  878. temp = expand_binop (mode,
  879. unsignedp ? umod_optab : smod_optab,
  880. op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  881. }
  882. else
  883. {
  884. /* We divided. Now finish doing X - Y * (X / Y). */
  885. temp = expand_mult (mode, temp, op1, temp, unsignedp);
  886. if (! temp) abort ();
  887. temp = expand_binop (mode, sub_optab, op0,
  888. temp, target, unsignedp, OPTAB_LIB_WIDEN);
  889. }
  890. }
  891. if (temp != 0)
  892. return temp;
  893. abort ();
  894. }