expmed.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /* Medium-level subroutines: convert bit-field store and extract
  2. and shifts, multiplies and divides to rtl instructions.
  3. Copyright (C) 1987, 1988 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 "flags.h"
  22. #include "insn-flags.h"
  23. #include "insn-codes.h"
  24. #include "insn-config.h"
  25. #include "expr.h"
  26. #include "recog.h"
  27. static rtx extract_split_bit_field ();
  28. static rtx extract_fixed_bit_field ();
  29. static void store_split_bit_field ();
  30. static void store_fixed_bit_field ();
  31. /* Return an rtx representing minus the value of X. */
  32. rtx
  33. negate_rtx (x)
  34. rtx x;
  35. {
  36. if (GET_CODE (x) == CONST_INT)
  37. return gen_rtx (CONST_INT, VOIDmode, - INTVAL (x));
  38. else
  39. return expand_unop (GET_MODE (x), neg_optab, x, 0, 0);
  40. }
  41. /* Generate code to store value from rtx VALUE
  42. into a bit-field within structure STR_RTX
  43. containing BITSIZE bits starting at bit BITNUM.
  44. FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
  45. /* ??? This should really have the ability to copy a word into a register
  46. in order to store the bit-field into it, on machines whose insv insns
  47. work that way. */
  48. rtx
  49. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value)
  50. rtx str_rtx;
  51. register int bitsize;
  52. int bitnum;
  53. enum machine_mode fieldmode;
  54. rtx value;
  55. {
  56. int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  57. register int offset = bitnum / unit;
  58. register int bitpos = bitnum % unit;
  59. register rtx op0 = str_rtx;
  60. rtx value1;
  61. if (GET_CODE (op0) == SUBREG)
  62. {
  63. offset += SUBREG_WORD (op0);
  64. op0 = SUBREG_REG (op0);
  65. }
  66. value = protect_from_queue (value, 0);
  67. if (flag_force_mem)
  68. value = force_not_mem (value);
  69. if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD)
  70. {
  71. /* Storing in a full-word or multi-word field in a register
  72. can be done with just SUBREG. */
  73. if (GET_MODE (op0) != fieldmode)
  74. op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  75. emit_move_insn (op0, value);
  76. return value;
  77. }
  78. #ifdef BYTES_BIG_ENDIAN
  79. /* If OP0 is a register, BITPOS must count within a word.
  80. But as we have it, it counts within whatever size OP0 now has.
  81. On a bigendian machine, these are not the same, so convert. */
  82. if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  83. bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  84. #endif
  85. /* Storing an lsb-aligned field in a register
  86. can be done with a movestrict instruction. */
  87. if (GET_CODE (op0) != MEM
  88. #ifdef BYTES_BIG_ENDIAN
  89. && bitpos + bitsize == unit
  90. #else
  91. && bitpos == 0
  92. #endif
  93. && (GET_MODE (op0) == fieldmode
  94. || (movstrict_optab->handlers[(int) fieldmode].insn_code
  95. != CODE_FOR_nothing)))
  96. {
  97. /* Get appropriate low part of the value being stored. */
  98. if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  99. value = gen_lowpart (fieldmode, value);
  100. else if (!(GET_CODE (value) == SYMBOL_REF
  101. || GET_CODE (value) == LABEL_REF
  102. || GET_CODE (value) == CONST))
  103. value = convert_to_mode (fieldmode, value, 0);
  104. if (GET_MODE (op0) == fieldmode)
  105. emit_move_insn (op0, value);
  106. else
  107. emit_insn (GEN_FCN (movstrict_optab->handlers[(int) fieldmode].insn_code)
  108. (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  109. return value;
  110. }
  111. /* From here on we can assume that the field to be stored in is an integer,
  112. since it is shorter than a word. */
  113. /* OFFSET is the number of words or bytes (UNIT says which)
  114. from STR_RTX to the first word or byte containing part of the field. */
  115. if (GET_CODE (op0) == REG)
  116. {
  117. if (offset != 0
  118. || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  119. op0 = gen_rtx (SUBREG, SImode, op0, offset);
  120. offset = 0;
  121. }
  122. else
  123. {
  124. op0 = protect_from_queue (op0, 1);
  125. }
  126. /* Now OFFSET is nonzero only if OP0 is memory
  127. and is therefore always measured in bytes. */
  128. #ifdef HAVE_insv
  129. if (HAVE_insv
  130. && !(bitsize == 1 && GET_CODE (value) == CONST_INT))
  131. {
  132. enum machine_mode mode0 = GET_MODE (op0);
  133. /* Add OFFSET into OP0's address. */
  134. if (GET_CODE (op0) == MEM)
  135. op0 = change_address (op0, QImode,
  136. plus_constant (XEXP (op0, 0), offset));
  137. /* If op0 is a register, we need it in SImode
  138. to make it acceptable to the format of insv. */
  139. if (GET_CODE (op0) == SUBREG)
  140. PUT_MODE (op0, SImode);
  141. if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  142. op0 = gen_rtx (SUBREG, SImode, op0, 0);
  143. /* Convert VALUE to SImode (which insv insn wants) in VALUE1. */
  144. value1 = value;
  145. if (GET_MODE (value) != SImode)
  146. {
  147. if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  148. {
  149. if (GET_CODE (value) != REG)
  150. value1 = copy_to_reg (value);
  151. /* Optimization: Don't bother really extending VALUE
  152. if it has all the bits we will actually use. */
  153. value1 = gen_rtx (SUBREG, SImode, value1, 0);
  154. }
  155. else if (!CONSTANT_P (value))
  156. /* Parse phase is supposed to make VALUE's data type
  157. match that of the component reference, which is a type
  158. at least as wide as the field; so VALUE should have
  159. a mode that corresponds to that type. */
  160. abort ();
  161. }
  162. /* If this machine's insv insists on a register,
  163. get VALUE1 into a register. */
  164. if (! (*insn_operand_predicate[(int) CODE_FOR_insv][3]) (value1, SImode))
  165. value1 = force_reg (SImode, value1);
  166. /* On big-endian machines, we count bits from the most significant.
  167. If the bit field insn does not, we must invert. */
  168. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  169. bitpos = unit - 1 - bitpos;
  170. #endif
  171. emit_insn (gen_insv (op0,
  172. gen_rtx (CONST_INT, VOIDmode, bitsize),
  173. gen_rtx (CONST_INT, VOIDmode, bitpos),
  174. value1));
  175. }
  176. else
  177. #endif
  178. /* Insv is not available; store using shifts and boolean ops. */
  179. store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
  180. return value;
  181. }
  182. /* Use shifts and boolean operations to store VALUE
  183. into a bit field of width BITSIZE
  184. in a memory location specified by OP0 except offset by OFFSET bytes.
  185. The field starts at position BITPOS within the byte.
  186. (If OP0 is a register, it may be SImode or a narrower mode,
  187. but BITPOS still counts within a full word,
  188. which is significant on bigendian machines.)
  189. Note that protect_from_queue has already been done on OP0 and VALUE. */
  190. static void
  191. store_fixed_bit_field (op0, offset, bitsize, bitpos, value)
  192. register rtx op0;
  193. register int offset, bitsize, bitpos;
  194. register rtx value;
  195. {
  196. register enum machine_mode mode;
  197. int total_bits = BITS_PER_WORD;
  198. rtx subtarget;
  199. int all_zero = 0;
  200. int all_one = 0;
  201. /* Add OFFSET to OP0's address (if it is in memory)
  202. and if a single byte contains the whole bit field
  203. change OP0 to a byte. */
  204. if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  205. {
  206. /* Special treatment for a bit field split across two registers. */
  207. if (bitsize + bitpos > BITS_PER_WORD)
  208. {
  209. store_split_bit_field (op0, bitsize, bitpos, value);
  210. return;
  211. }
  212. }
  213. else if (bitsize + bitpos <= BITS_PER_UNIT
  214. && ! SLOW_BYTE_ACCESS)
  215. {
  216. total_bits = BITS_PER_UNIT;
  217. op0 = change_address (op0, QImode,
  218. plus_constant (XEXP (op0, 0), offset));
  219. }
  220. else
  221. {
  222. /* Get ref to word containing the field. */
  223. /* Adjust BITPOS to be position within a word,
  224. and OFFSET to be the offset of that word.
  225. Then alter OP0 to refer to that word. */
  226. bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  227. offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  228. op0 = change_address (op0, SImode,
  229. plus_constant (XEXP (op0, 0), offset));
  230. /* Special treatment for a bit field split across two words. */
  231. if (bitsize + bitpos > BITS_PER_WORD)
  232. {
  233. store_split_bit_field (op0, bitsize, bitpos, value);
  234. return;
  235. }
  236. }
  237. mode = GET_MODE (op0);
  238. /* Now OP0 is either a byte or a word, and the bit field is contained
  239. entirely within it. TOTAL_BITS and MODE say which one (byte or word).
  240. BITPOS is the starting bit number within the byte or word.
  241. (If OP0 is a word, it may actually have a mode narrower than SImode.) */
  242. #ifdef BYTES_BIG_ENDIAN
  243. /* BITPOS is the distance between our msb
  244. and that of the containing byte or word.
  245. Convert it to the distance from the lsb. */
  246. bitpos = total_bits - bitsize - bitpos;
  247. #endif
  248. /* Now BITPOS is always the distance between our lsb
  249. and that of the containing byte or word. */
  250. /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
  251. we must first convert its mode to MODE. */
  252. if (GET_CODE (value) == CONST_INT)
  253. {
  254. register int v = INTVAL (value);
  255. if (bitsize < HOST_BITS_PER_INT)
  256. v &= (1 << bitsize) - 1;
  257. if (v == 0)
  258. all_zero = 1;
  259. else if (bitsize < HOST_BITS_PER_INT && v == (1 << bitsize) - 1)
  260. all_one = 1;
  261. value = gen_rtx (CONST_INT, VOIDmode, v << bitpos);
  262. }
  263. else
  264. {
  265. int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize);
  266. if (GET_MODE (value) != mode)
  267. {
  268. if (GET_CODE (value) == REG && mode == QImode)
  269. value = gen_rtx (SUBREG, mode, value, 0);
  270. else
  271. value = convert_to_mode (mode, value, 1);
  272. }
  273. if (must_and && bitsize < HOST_BITS_PER_INT)
  274. value = expand_bit_and (mode, value,
  275. gen_rtx (CONST_INT, VOIDmode,
  276. (1 << bitsize) - 1),
  277. 0);
  278. if (bitpos > 0)
  279. value = expand_shift (LSHIFT_EXPR, mode, value,
  280. build_int_2 (bitpos, 0), 0, 1);
  281. }
  282. /* Now clear the chosen bits in OP0,
  283. except that if VALUE is -1 we need not bother. */
  284. subtarget = op0;
  285. if (! all_one)
  286. subtarget = expand_bit_and (mode, op0,
  287. gen_rtx (CONST_INT, VOIDmode,
  288. (~ (((1 << bitsize) - 1) << bitpos))
  289. & ((1 << GET_MODE_BITSIZE (mode)) - 1)),
  290. subtarget);
  291. /* Now logical-or VALUE into OP0, unless it is zero. */
  292. if (! all_zero)
  293. subtarget = expand_binop (mode, ior_optab, subtarget, value,
  294. op0, 1, OPTAB_LIB_WIDEN);
  295. if (op0 != subtarget)
  296. emit_move_insn (op0, subtarget);
  297. }
  298. /* Store a bit field that is split across two words.
  299. OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  300. BITSIZE is the field width; BITPOS the position of its first bit
  301. (within the word).
  302. VALUE is the value to store. */
  303. static void
  304. store_split_bit_field (op0, bitsize, bitpos, value)
  305. rtx op0;
  306. int bitsize, bitpos;
  307. rtx value;
  308. {
  309. /* BITSIZE_1 is size of the part in the first word. */
  310. int bitsize_1 = BITS_PER_WORD - bitpos;
  311. /* BITSIZE_2 is size of the rest (in the following word). */
  312. int bitsize_2 = bitsize - bitsize_1;
  313. rtx part1, part2;
  314. if (GET_MODE (value) != VOIDmode)
  315. value = convert_to_mode (SImode, value, 1);
  316. if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  317. value = copy_to_reg (value);
  318. /* Split the value into two parts:
  319. PART1 gets that which goes in the first word; PART2 the other. */
  320. #ifdef BYTES_BIG_ENDIAN
  321. /* PART1 gets the more significant part. */
  322. if (GET_CODE (value) == CONST_INT)
  323. {
  324. part1 = gen_rtx (CONST_INT, VOIDmode,
  325. (unsigned) (INTVAL (value)) >> bitsize_2);
  326. part2 = gen_rtx (CONST_INT, VOIDmode,
  327. (unsigned) (INTVAL (value)) & ((1 << bitsize_2) - 1));
  328. }
  329. else
  330. {
  331. part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1,
  332. BITS_PER_WORD - bitsize, 0, 1);
  333. part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  334. BITS_PER_WORD - bitsize_2, 0, 1);
  335. }
  336. #else
  337. /* PART1 gets the less significant part. */
  338. if (GET_CODE (value) == CONST_INT)
  339. {
  340. part1 = gen_rtx (CONST_INT, VOIDmode,
  341. (unsigned) (INTVAL (value)) & ((1 << bitsize_1) - 1));
  342. part2 = gen_rtx (CONST_INT, VOIDmode,
  343. (unsigned) (INTVAL (value)) >> bitsize_1);
  344. }
  345. else
  346. {
  347. part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1, 0, 0, 1);
  348. part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  349. bitsize_1, 0, 1);
  350. }
  351. #endif
  352. /* Store PART1 into the first word. */
  353. store_fixed_bit_field (op0, 0, bitsize_1, bitpos, part1);
  354. /* Offset op0 to get to the following word. */
  355. if (GET_CODE (op0) == MEM)
  356. op0 = change_address (op0, SImode,
  357. plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  358. else if (GET_CODE (op0) == REG)
  359. op0 = gen_rtx (SUBREG, SImode, op0, 1);
  360. else
  361. op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  362. /* Store PART2 into the second word. */
  363. store_fixed_bit_field (op0, 0, bitsize_2, 0, part2);
  364. }
  365. /* Generate code to extract a byte-field from STR_RTX
  366. containing BITSIZE bits, starting at BITNUM,
  367. and put it in TARGET if possible (if TARGET is nonzero).
  368. Regardless of TARGET, we return the rtx for where the value is placed.
  369. It may be a QUEUED.
  370. STR_RTX is the structure containing the byte (a REG or MEM).
  371. UNSIGNEDP is nonzero if this is an unsigned bit field.
  372. MODE is the natural mode of the field value once extracted.
  373. TMODE is the mode the caller would like the value to have;
  374. but the value may be returned with type MODE instead.
  375. If a TARGET is specified and we can store in it at no extra cost,
  376. we do so, and return TARGET.
  377. Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  378. if they are equally easy. */
  379. rtx
  380. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp, target, mode, tmode)
  381. rtx str_rtx;
  382. register int bitsize;
  383. int bitnum;
  384. int unsignedp;
  385. rtx target;
  386. enum machine_mode mode, tmode;
  387. {
  388. int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  389. register int offset = bitnum / unit;
  390. register int bitpos = bitnum % unit;
  391. register rtx op0 = str_rtx;
  392. rtx spec_target = target;
  393. rtx bitsize_rtx, bitpos_rtx;
  394. rtx spec_target_subreg = 0;
  395. if (tmode == VOIDmode)
  396. tmode = mode;
  397. while (GET_CODE (op0) == SUBREG)
  398. {
  399. offset += SUBREG_WORD (op0);
  400. op0 = SUBREG_REG (op0);
  401. }
  402. #ifdef BYTES_BIG_ENDIAN
  403. /* If OP0 is a register, BITPOS must count within a word.
  404. But as we have it, it counts within whatever size OP0 now has.
  405. On a bigendian machine, these are not the same, so convert. */
  406. if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  407. bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  408. #endif
  409. /* Extracting a full-word or multi-word value
  410. from a structure in a register.
  411. This can be done with just SUBREG.
  412. So too extracting a subword value in
  413. the least significant part of the register. */
  414. if (GET_CODE (op0) == REG
  415. && (bitsize >= BITS_PER_WORD
  416. || ((bitsize == GET_MODE_BITSIZE (mode)
  417. || bitsize == GET_MODE_BITSIZE (QImode)
  418. || bitsize == GET_MODE_BITSIZE (HImode))
  419. #ifdef BYTES_BIG_ENDIAN
  420. && bitpos + bitsize == BITS_PER_WORD
  421. #else
  422. && bitpos == 0
  423. #endif
  424. )))
  425. {
  426. enum machine_mode mode1 = mode;
  427. if (bitsize == GET_MODE_BITSIZE (QImode))
  428. mode1 = QImode;
  429. if (bitsize == GET_MODE_BITSIZE (HImode))
  430. mode1 = HImode;
  431. if (mode1 != GET_MODE (op0))
  432. op0 = gen_rtx (SUBREG, mode1, op0, offset);
  433. if (mode1 != mode)
  434. return convert_to_mode (tmode, op0, unsignedp);
  435. return op0;
  436. }
  437. /* From here on we know the desired field is smaller than a word
  438. so we can assume it is an integer. So we can safely extract it as one
  439. size of integer, if necessary, and then truncate or extend
  440. to the size that is wanted. */
  441. /* OFFSET is the number of words or bytes (UNIT says which)
  442. from STR_RTX to the first word or byte containing part of the field. */
  443. if (GET_CODE (op0) == REG)
  444. {
  445. if (offset != 0
  446. || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  447. op0 = gen_rtx (SUBREG, SImode, op0, offset);
  448. offset = 0;
  449. }
  450. else
  451. {
  452. op0 = protect_from_queue (str_rtx, 1);
  453. }
  454. /* Now OFFSET is nonzero only for memory operands. */
  455. if (unsignedp)
  456. {
  457. #ifdef HAVE_extzv
  458. if (HAVE_extzv)
  459. {
  460. /* Get ref to first byte containing part of the field. */
  461. if (GET_CODE (op0) == MEM)
  462. op0 = change_address (op0, QImode,
  463. plus_constant (XEXP (op0, 0), offset));
  464. /* If op0 is a register, we need it in SImode
  465. to make it acceptable to the format of extv. */
  466. if (GET_CODE (op0) == SUBREG)
  467. PUT_MODE (op0, SImode);
  468. if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  469. op0 = gen_rtx (SUBREG, SImode, op0, 0);
  470. if (target == 0
  471. || (flag_force_mem && GET_CODE (target) == MEM))
  472. target = spec_target = gen_reg_rtx (tmode);
  473. if (GET_MODE (target) != SImode)
  474. {
  475. if (GET_CODE (target) == REG)
  476. spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  477. else
  478. target = gen_reg_rtx (SImode);
  479. }
  480. /* If this machine's extzv insists on a register target,
  481. make sure we have one. */
  482. if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (target, SImode))
  483. target = gen_reg_rtx (SImode);
  484. /* On big-endian machines, we count bits from the most significant.
  485. If the bit field insn does not, we must invert. */
  486. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  487. bitpos = unit - 1 - bitpos;
  488. #endif
  489. bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  490. bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  491. emit_insn (gen_extzv (protect_from_queue (target, 1),
  492. op0, bitsize_rtx, bitpos_rtx));
  493. }
  494. else
  495. #endif
  496. target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  497. target, 1);
  498. }
  499. else
  500. {
  501. #ifdef HAVE_extv
  502. if (HAVE_extv)
  503. {
  504. /* Get ref to first byte containing part of the field. */
  505. if (GET_CODE (op0) == MEM)
  506. op0 = change_address (op0, QImode,
  507. plus_constant (XEXP (op0, 0), offset));
  508. /* If op0 is a register, we need it in QImode
  509. to make it acceptable to the format of extv. */
  510. if (GET_CODE (op0) == SUBREG)
  511. PUT_MODE (op0, SImode);
  512. if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  513. op0 = gen_rtx (SUBREG, SImode, op0, 0);
  514. if (target == 0
  515. || (flag_force_mem && GET_CODE (target) == MEM))
  516. target = spec_target = gen_reg_rtx (tmode);
  517. if (GET_MODE (target) != SImode)
  518. {
  519. if (GET_CODE (target) == REG)
  520. spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  521. else
  522. target = gen_reg_rtx (SImode);
  523. }
  524. /* If this machine's extv insists on a register target,
  525. make sure we have one. */
  526. if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (target, SImode))
  527. target = gen_reg_rtx (SImode);
  528. /* On big-endian machines, we count bits from the most significant.
  529. If the bit field insn does not, we must invert. */
  530. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  531. bitpos = unit - 1 - bitpos;
  532. #endif
  533. bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  534. bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  535. emit_insn (gen_extv (protect_from_queue (target, 1), op0,
  536. bitsize_rtx, bitpos_rtx));
  537. }
  538. else
  539. #endif
  540. target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  541. target, 0);
  542. }
  543. if (target == spec_target)
  544. return target;
  545. if (target == spec_target_subreg)
  546. return spec_target;
  547. if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  548. return convert_to_mode (tmode, target, unsignedp);
  549. return target;
  550. }
  551. /* Extract a bit field using shifts and boolean operations
  552. Returns an rtx to represent the value.
  553. OP0 addresses a register (word) or memory (byte).
  554. BITPOS says which bit within the word or byte the bit field starts in.
  555. OFFSET says how many bytes farther the bit field starts;
  556. it is 0 if OP0 is a register.
  557. BITSIZE says how many bits long the bit field is.
  558. (If OP0 is a register, it may be narrower than SImode,
  559. but BITPOS still counts within a full word,
  560. which is significant on bigendian machines.)
  561. UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  562. If TARGET is nonzero, attempts to store the value there
  563. and return TARGET, but this is not guaranteed.
  564. If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
  565. static rtx
  566. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos, target, unsignedp)
  567. enum machine_mode tmode;
  568. register rtx op0, target;
  569. register int offset, bitsize, bitpos;
  570. int unsignedp;
  571. {
  572. int total_bits = BITS_PER_WORD;
  573. enum machine_mode mode;
  574. rtx orig = op0;
  575. if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  576. {
  577. /* Special treatment for a bit field split across two registers. */
  578. if (bitsize + bitpos > BITS_PER_WORD)
  579. return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
  580. }
  581. else if (bitsize + bitpos <= BITS_PER_UNIT && ! SLOW_BYTE_ACCESS)
  582. {
  583. /* If the bit field fits entirely in one byte of memory,
  584. let OP0 be that byte. We must add OFFSET to its address. */
  585. total_bits = BITS_PER_UNIT;
  586. op0 = change_address (op0, QImode,
  587. plus_constant (XEXP (op0, 0), offset));
  588. }
  589. else
  590. {
  591. #ifdef STRICT_ALIGNMENT
  592. #ifndef STRUCTURE_SIZE_BOUNDARY
  593. /* The following code assumes that OP0 is aligned
  594. such that a word can be fetched there.
  595. This could be because words don't need to be aligned,
  596. or because all structures are suitably aligned. */
  597. abort ();
  598. #endif
  599. #endif
  600. /* Get ref to word containing the field. */
  601. /* Adjust BITPOS to be position within a word,
  602. and OFFSET to be the offset of that word. */
  603. bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  604. offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  605. op0 = change_address (op0, SImode,
  606. plus_constant (XEXP (op0, 0), offset));
  607. /* Special treatment for a bit field split across two words. */
  608. if (bitsize + bitpos > BITS_PER_WORD)
  609. return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
  610. }
  611. mode = GET_MODE (op0);
  612. #ifdef BYTES_BIG_ENDIAN
  613. /* BITPOS is the distance between our msb
  614. and that of the containing byte or word.
  615. Convert it to the distance from the lsb. */
  616. bitpos = total_bits - bitsize - bitpos;
  617. #endif
  618. /* Now BITPOS is always the distance between our lsb
  619. and that of the containing byte or word.
  620. We have reduced the big-endian case to the little-endian case. */
  621. if (unsignedp)
  622. {
  623. if (bitpos)
  624. {
  625. /* If the field does not already start at the lsb,
  626. shift it so it does. */
  627. tree amount = build_int_2 (bitpos, 0);
  628. /* Maybe propagate the target for the shift. */
  629. /* Certainly do so if we will return the value of the shift. */
  630. rtx subtarget = (target != 0 && GET_CODE (target) == REG
  631. && FUNCTION_VALUE_REGNO_P (REGNO (target))
  632. ? target : 0);
  633. if (tmode != mode) subtarget = 0;
  634. op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  635. }
  636. /* Convert the value to the desired mode. */
  637. if (mode != tmode)
  638. op0 = convert_to_mode (tmode, op0, 1);
  639. /* Unless the msb of the field used to be the msb when we shifted,
  640. mask out the upper bits. */
  641. if ((GET_MODE_BITSIZE (mode) != bitpos + bitsize
  642. #if 0
  643. #ifdef SLOW_ZERO_EXTEND
  644. /* Always generate an `and' if
  645. we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  646. will combine fruitfully with the zero-extend. */
  647. || tmode != mode
  648. #endif
  649. #endif
  650. )
  651. && bitsize < HOST_BITS_PER_INT)
  652. return expand_bit_and (GET_MODE (op0), op0,
  653. gen_rtx (CONST_INT, VOIDmode, (1 << bitsize) - 1),
  654. target);
  655. return op0;
  656. }
  657. /* To extract a signed bit-field, first shift its msb to the msb of the word,
  658. then arithmetic-shift its lsb to the lsb of the word. */
  659. op0 = force_reg (mode, op0);
  660. if (mode != tmode)
  661. target = 0;
  662. if (GET_MODE_BITSIZE (QImode) < GET_MODE_BITSIZE (mode)
  663. && GET_MODE_BITSIZE (QImode) >= bitsize + bitpos)
  664. mode = QImode, op0 = convert_to_mode (QImode, op0, 0);
  665. if (GET_MODE_BITSIZE (HImode) < GET_MODE_BITSIZE (mode)
  666. && GET_MODE_BITSIZE (HImode) >= bitsize + bitpos)
  667. mode = HImode, op0 = convert_to_mode (HImode, op0, 0);
  668. if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  669. {
  670. tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  671. /* Maybe propagate the target for the shift. */
  672. /* Certainly do so if we will return the value of the shift. */
  673. rtx subtarget = (target != 0 && GET_CODE (target) == REG
  674. && FUNCTION_VALUE_REGNO_P (REGNO (target))
  675. ? target : 0);
  676. op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  677. }
  678. return expand_shift (RSHIFT_EXPR, mode, op0,
  679. build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
  680. target, 0);
  681. }
  682. /* Extract a bit field that is split across two words
  683. and return an RTX for the result.
  684. OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  685. BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  686. UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
  687. static rtx
  688. extract_split_bit_field (op0, bitsize, bitpos, unsignedp)
  689. rtx op0;
  690. int bitsize, bitpos, unsignedp;
  691. {
  692. /* BITSIZE_1 is size of the part in the first word. */
  693. int bitsize_1 = BITS_PER_WORD - bitpos;
  694. /* BITSIZE_2 is size of the rest (in the following word). */
  695. int bitsize_2 = bitsize - bitsize_1;
  696. rtx part1, part2, result;
  697. /* Get the part of the bit field from the first word. */
  698. part1 = extract_fixed_bit_field (SImode, op0, 0, bitsize_1, bitpos, 0, 1);
  699. /* Offset op0 by 1 word to get to the following one. */
  700. if (GET_CODE (op0) == MEM)
  701. op0 = change_address (op0, SImode,
  702. plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  703. else if (GET_CODE (op0) == REG)
  704. op0 = gen_rtx (SUBREG, SImode, op0, 1);
  705. else
  706. op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  707. /* Get the part of the bit field from the second word. */
  708. part2 = extract_fixed_bit_field (SImode, op0, 0, bitsize_2, 0, 0, 1);
  709. /* Shift the more significant part up to fit above the other part. */
  710. #ifdef BYTES_BIG_ENDIAN
  711. part1 = expand_shift (LSHIFT_EXPR, SImode, part1,
  712. build_int_2 (bitsize_2, 0), 0, 1);
  713. #else
  714. part2 = expand_shift (LSHIFT_EXPR, SImode, part2,
  715. build_int_2 (bitsize_1, 0), 0, 1);
  716. #endif
  717. /* Combine the two parts with bitwise or. This works
  718. because we extracted both parts as unsigned bit fields. */
  719. result = expand_binop (SImode, ior_optab, part1, part2, 0, 1,
  720. OPTAB_LIB_WIDEN);
  721. /* Unsigned bit field: we are done. */
  722. if (unsignedp)
  723. return result;
  724. /* Signed bit field: sign-extend with two arithmetic shifts. */
  725. result = expand_shift (LSHIFT_EXPR, SImode, result,
  726. build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  727. return expand_shift (RSHIFT_EXPR, SImode, result,
  728. build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  729. }
  730. /* Add INC into TARGET. */
  731. void
  732. expand_inc (target, inc)
  733. rtx target, inc;
  734. {
  735. rtx value = expand_binop (GET_MODE (target), add_optab,
  736. target, inc,
  737. target, 0, OPTAB_LIB_WIDEN);
  738. if (value != target)
  739. emit_move_insn (target, value);
  740. }
  741. /* Subtract INC from TARGET. */
  742. void
  743. expand_dec (target, dec)
  744. rtx target, dec;
  745. {
  746. rtx value = expand_binop (GET_MODE (target), sub_optab,
  747. target, dec,
  748. target, 0, OPTAB_LIB_WIDEN);
  749. if (value != target)
  750. emit_move_insn (target, value);
  751. }
  752. /* Output a shift instruction for expression code CODE,
  753. with SHIFTED being the rtx for the value to shift,
  754. and AMOUNT the tree for the amount to shift by.
  755. Store the result in the rtx TARGET, if that is convenient.
  756. If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  757. Return the rtx for where the value is. */
  758. /* Pastel, for shifts, converts shift count to SImode here
  759. independent of the mode being shifted.
  760. Should that be done in an earlier pass?
  761. It turns out not to matter for C. */
  762. rtx
  763. expand_shift (code, mode, shifted, amount, target, unsignedp)
  764. enum tree_code code;
  765. register enum machine_mode mode;
  766. rtx shifted;
  767. tree amount;
  768. register rtx target;
  769. int unsignedp;
  770. {
  771. register rtx op1, temp = 0;
  772. register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  773. int try;
  774. rtx negated = 0;
  775. int rotate = code == LROTATE_EXPR || code == RROTATE_EXPR;
  776. /* Previously detected shift-counts computed by NEGATE_EXPR
  777. and shifted in the other direction; but that does not work
  778. on all machines. */
  779. op1 = expand_expr (amount, 0, VOIDmode, 0);
  780. for (try = 0; temp == 0 && try < 3; try++)
  781. {
  782. enum optab_methods methods;
  783. if (try == 0)
  784. methods = OPTAB_DIRECT;
  785. else if (try == 1)
  786. methods = OPTAB_WIDEN;
  787. else
  788. methods = OPTAB_LIB_WIDEN;
  789. if (rotate)
  790. {
  791. /* Widening does not work for rotation. */
  792. if (methods != OPTAB_DIRECT)
  793. methods = OPTAB_LIB;
  794. temp = expand_binop (mode,
  795. left ? rotl_optab : rotr_optab,
  796. shifted, op1, target, -1, methods);
  797. /* If there is no shift instruction for the desired direction,
  798. try negating the shift count and shifting in the other direction.
  799. If a machine has only a left shift instruction then we are
  800. entitled to assume it shifts right with negative args. */
  801. if (temp == 0)
  802. {
  803. if (negated != 0)
  804. ;
  805. else if (GET_CODE (op1) == CONST_INT)
  806. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  807. else
  808. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  809. temp = expand_binop (mode,
  810. left ? rotr_optab : rotl_optab,
  811. shifted, negated, target, -1, methods);
  812. }
  813. }
  814. else if (unsignedp)
  815. {
  816. temp = expand_binop (mode,
  817. left ? lshl_optab : lshr_optab,
  818. shifted, op1, target, unsignedp, methods);
  819. if (temp == 0 && left)
  820. temp = expand_binop (mode, ashl_optab,
  821. shifted, op1, target, unsignedp, methods);
  822. if (temp == 0)
  823. {
  824. if (negated != 0)
  825. ;
  826. else if (GET_CODE (op1) == CONST_INT)
  827. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  828. else
  829. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  830. temp = expand_binop (mode,
  831. left ? lshr_optab : lshl_optab,
  832. shifted, negated,
  833. target, unsignedp, methods);
  834. }
  835. if (temp != 0)
  836. return temp;
  837. }
  838. /* Do arithmetic shifts.
  839. Also, if we are going to widen the operand, we can just as well
  840. use an arithmetic right-shift instead of a logical one. */
  841. if (! rotate && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  842. {
  843. /* Arithmetic shift */
  844. temp = expand_binop (mode,
  845. left ? ashl_optab : ashr_optab,
  846. shifted, op1, target, unsignedp, methods);
  847. if (temp == 0)
  848. {
  849. if (negated != 0)
  850. ;
  851. else if (GET_CODE (op1) == CONST_INT)
  852. negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  853. else
  854. negated = expand_unop (mode, neg_optab, op1, 0, 0);
  855. temp = expand_binop (mode,
  856. left ? ashr_optab : ashl_optab,
  857. shifted, negated, target, unsignedp, methods);
  858. }
  859. if (temp != 0)
  860. return temp;
  861. }
  862. if (unsignedp)
  863. {
  864. /* No logical shift insn in either direction =>
  865. try a bit-field extract instruction if we have one. */
  866. #ifdef HAVE_extzv
  867. #ifndef BITS_BIG_ENDIAN
  868. if (HAVE_extzv && !left
  869. && ((methods == OPTAB_DIRECT && mode == SImode)
  870. || (methods == OPTAB_WIDEN
  871. && GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))))
  872. {
  873. rtx shifted1 = convert_to_mode (SImode, shifted, 1);
  874. rtx target1 = target;
  875. /* If -fforce-mem, don't let the operand be in memory. */
  876. if (flag_force_mem && GET_CODE (shifted1) == MEM)
  877. shifted1 = force_not_mem (shifted1);
  878. /* If this machine's extzv insists on a register for
  879. operand 1, arrange for that. */
  880. if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  881. (shifted1, SImode)))
  882. shifted1 = force_reg (SImode, shifted1);
  883. /* If we don't have or cannot use a suggested target,
  884. make a place for the result, in the proper mode. */
  885. if (methods == OPTAB_WIDEN || target1 == 0
  886. || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  887. (target1, SImode)))
  888. target1 = gen_reg_rtx (SImode);
  889. /* If this machine's extzv insists on a register for
  890. operand 3, arrange for that. */
  891. if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  892. (op1, SImode)))
  893. op1 = force_reg (SImode, op1);
  894. op1 = protect_from_queue (op1, 1);
  895. /* TEMP gets the width of the bit field to extract:
  896. wordsize minus # bits to shift by. */
  897. if (GET_CODE (op1) == CONST_INT)
  898. temp = gen_rtx (CONST_INT, VOIDmode,
  899. (GET_MODE_BITSIZE (mode) - INTVAL (op1)));
  900. else
  901. temp = expand_binop (SImode, sub_optab,
  902. gen_rtx (CONST_INT, VOIDmode,
  903. GET_MODE_BITSIZE (mode)),
  904. op1, gen_reg_rtx (SImode),
  905. 0, OPTAB_LIB_WIDEN);
  906. /* Now extract with width TEMP, omitting OP1 least sig bits. */
  907. emit_insn (gen_extzv (protect_from_queue (target1, 1),
  908. protect_from_queue (shifted1, 0),
  909. temp, op1));
  910. return convert_to_mode (mode, target1, 1);
  911. }
  912. /* Can also do logical shift with signed bit-field extract
  913. followed by inserting the bit-field at a different position.
  914. That strategy is not yet implemented. */
  915. #endif /* not BITS_BIG_ENDIAN */
  916. #endif /* HAVE_extzv */
  917. /* We have failed to generate the logical shift and will abort. */
  918. }
  919. }
  920. if (temp == 0)
  921. abort ();
  922. return temp;
  923. }
  924. /* Output an instruction or two to bitwise-and OP0 with OP1
  925. in mode MODE, with output to TARGET if convenient and TARGET is not zero.
  926. Returns where the result is. */
  927. rtx
  928. expand_bit_and (mode, op0, op1, target)
  929. enum machine_mode mode;
  930. rtx op0, op1, target;
  931. {
  932. register rtx temp;
  933. /* First try to open-code it directly. */
  934. temp = expand_binop (mode, and_optab, op0, op1, target, 1, OPTAB_DIRECT);
  935. if (temp == 0)
  936. {
  937. rtx compl;
  938. /* If that fails, try to open code using a clear-bits insn. */
  939. if (GET_CODE (op1) == CONST_INT
  940. && GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  941. compl = gen_rtx (CONST_INT, VOIDmode,
  942. ((1 << GET_MODE_BITSIZE (mode)) - 1) & ~INTVAL (op1));
  943. else
  944. compl = expand_unop (mode, one_cmpl_optab, op1, 0, 1);
  945. temp = expand_binop (mode, andcb_optab, op0, compl, target,
  946. 1, OPTAB_DIRECT);
  947. }
  948. if (temp == 0)
  949. /* If still no luck, try library call or wider modes. */
  950. temp = expand_binop (mode, and_optab, op0, op1, target,
  951. 1, OPTAB_LIB_WIDEN);
  952. if (temp == 0)
  953. abort ();
  954. return temp;
  955. }
  956. /* Perform a multiplication and return an rtx for the result.
  957. MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  958. TARGET is a suggestion for where to store the result (an rtx).
  959. We check specially for a constant integer as OP1.
  960. If you want this check for OP0 as well, then before calling
  961. you should swap the two operands if OP0 would be constant. */
  962. rtx
  963. expand_mult (mode, op0, op1, target, unsignedp)
  964. enum machine_mode mode;
  965. register rtx op0, op1, target;
  966. int unsignedp;
  967. {
  968. /* Don't use the function value register as a target
  969. since we have to read it as well as write it,
  970. and function-inlining gets confused by this. */
  971. if (target && REG_P (target) && FUNCTION_VALUE_REGNO_P (REGNO (target)))
  972. target = 0;
  973. if (GET_CODE (op1) == CONST_INT)
  974. {
  975. register int foo;
  976. int bar;
  977. int negate = INTVAL (op1) < 0;
  978. int absval = INTVAL (op1) * (negate ? -1 : 1);
  979. /* Is multiplier a power of 2, or minus that? */
  980. foo = exact_log2 (absval);
  981. if (foo >= 0)
  982. {
  983. rtx tem = expand_shift (LSHIFT_EXPR, mode, op0,
  984. build_int_2 (foo, 0),
  985. target, 0);
  986. return negate ? negate_rtx (tem) : tem;
  987. }
  988. /* Is multiplier a sum of two powers of 2, or minus that? */
  989. bar = floor_log2 (absval);
  990. foo = exact_log2 (absval - (1 << bar));
  991. if (bar >= 0 && foo >= 0)
  992. {
  993. rtx pow1 = ((foo == 0) ? op0
  994. : expand_shift (LSHIFT_EXPR, mode, op0,
  995. build_int_2 (foo, 0),
  996. 0, 0));
  997. rtx pow2 = expand_shift (LSHIFT_EXPR, mode, op0,
  998. build_int_2 (bar, 0),
  999. 0, 0);
  1000. rtx tem = force_operand (gen_rtx (PLUS, mode, pow1, pow2), target);
  1001. return negate ? negate_rtx (tem) : tem;
  1002. }
  1003. }
  1004. /* This used to use umul_optab if unsigned,
  1005. but I think that for non-widening multiply there is no difference
  1006. between signed and unsigned. */
  1007. op0 = expand_binop (mode, smul_optab,
  1008. op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  1009. if (op0 == 0)
  1010. abort ();
  1011. return op0;
  1012. }
  1013. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  1014. if that is convenient, and returning where the result is.
  1015. You may request either the quotient or the remainder as the result;
  1016. specify REM_FLAG nonzero to get the remainder.
  1017. CODE is the expression code for which kind of division this is;
  1018. it controls how rounding is done. MODE is the machine mode to use.
  1019. UNSIGNEDP nonzero means do unsigned division. */
  1020. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  1021. and then correct it by or'ing in missing high bits
  1022. if result of ANDI is nonzero.
  1023. For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  1024. This could optimize to a bfexts instruction.
  1025. But C doesn't use these operations, so their optimizations are
  1026. left for later. */
  1027. rtx
  1028. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  1029. int rem_flag;
  1030. enum tree_code code;
  1031. enum machine_mode mode;
  1032. register rtx op0, op1, target;
  1033. int unsignedp;
  1034. {
  1035. register rtx label;
  1036. register rtx temp;
  1037. int log = -1;
  1038. int can_clobber_op0;
  1039. int mod_insn_no_good = 0;
  1040. rtx adjusted_op0 = op0;
  1041. /* Don't use the function value register as a target
  1042. since we have to read it as well as write it,
  1043. and function-inlining gets confused by this. */
  1044. if (target && REG_P (target) && FUNCTION_VALUE_REGNO_P (REGNO (target)))
  1045. target = 0;
  1046. if (target == 0)
  1047. {
  1048. target = gen_reg_rtx (mode);
  1049. }
  1050. /* Don't clobber an operand while doing a multi-step calculation. */
  1051. if ((rem_flag && rtx_equal_p (target, op0))
  1052. || rtx_equal_p (target, op1))
  1053. target = gen_reg_rtx (mode);
  1054. can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  1055. if (GET_CODE (op1) == CONST_INT)
  1056. log = exact_log2 (INTVAL (op1));
  1057. /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  1058. which is really floor-division. Otherwise we will really do a divide,
  1059. and we assume that is trunc-division.
  1060. We must correct the dividend by adding or subtracting something
  1061. based on the divisor, in order to do the kind of rounding specified
  1062. by CODE. The correction depends on what kind of rounding is actually
  1063. available, and that depends on whether we will shift or divide. */
  1064. switch (code)
  1065. {
  1066. case TRUNC_MOD_EXPR:
  1067. case TRUNC_DIV_EXPR:
  1068. if (log >= 0 && ! unsignedp)
  1069. {
  1070. label = gen_label_rtx ();
  1071. if (! can_clobber_op0)
  1072. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1073. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1074. emit_jump_insn (gen_bge (label));
  1075. expand_inc (adjusted_op0, plus_constant (op1, -1));
  1076. emit_label (label);
  1077. mod_insn_no_good = 1;
  1078. }
  1079. break;
  1080. case FLOOR_DIV_EXPR:
  1081. case FLOOR_MOD_EXPR:
  1082. if (log < 0 && ! unsignedp)
  1083. {
  1084. label = gen_label_rtx ();
  1085. if (! can_clobber_op0)
  1086. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1087. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1088. emit_jump_insn (gen_bge (label));
  1089. expand_dec (adjusted_op0, op1);
  1090. expand_inc (adjusted_op0, const1_rtx);
  1091. emit_label (label);
  1092. mod_insn_no_good = 1;
  1093. }
  1094. break;
  1095. case CEIL_DIV_EXPR:
  1096. case CEIL_MOD_EXPR:
  1097. if (! can_clobber_op0)
  1098. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1099. if (log < 0)
  1100. {
  1101. if (! unsignedp)
  1102. {
  1103. label = gen_label_rtx ();
  1104. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1105. emit_jump_insn (gen_ble (label));
  1106. }
  1107. expand_inc (adjusted_op0, op1);
  1108. expand_dec (adjusted_op0, const1_rtx);
  1109. if (! unsignedp)
  1110. emit_label (label);
  1111. }
  1112. else
  1113. {
  1114. adjusted_op0 = expand_binop (GET_MODE (target), add_optab,
  1115. adjusted_op0, plus_constant (op1, -1),
  1116. 0, 0, OPTAB_LIB_WIDEN);
  1117. }
  1118. mod_insn_no_good = 1;
  1119. break;
  1120. case ROUND_DIV_EXPR:
  1121. case ROUND_MOD_EXPR:
  1122. if (! can_clobber_op0)
  1123. adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1124. if (log < 0)
  1125. {
  1126. op1 = expand_shift (RSHIFT_EXPR, mode, op1, integer_one_node, 0, 0);
  1127. if (! unsignedp)
  1128. {
  1129. label = gen_label_rtx ();
  1130. emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1131. emit_jump_insn (gen_bge (label));
  1132. expand_unop (mode, neg_optab, op1, op1, 0);
  1133. emit_label (label);
  1134. }
  1135. expand_inc (adjusted_op0, op1);
  1136. }
  1137. else
  1138. {
  1139. op1 = gen_rtx (CONST_INT, VOIDmode, INTVAL (op1) / 2);
  1140. expand_inc (adjusted_op0, op1);
  1141. }
  1142. mod_insn_no_good = 1;
  1143. break;
  1144. }
  1145. if (rem_flag && !mod_insn_no_good)
  1146. {
  1147. /* Try to produce the remainder directly */
  1148. if (log >= 0)
  1149. {
  1150. return expand_bit_and (mode, adjusted_op0,
  1151. gen_rtx (CONST_INT, VOIDmode,
  1152. INTVAL (op1) - 1),
  1153. target);
  1154. }
  1155. else
  1156. {
  1157. /* See if we can do remainder without a library call. */
  1158. temp = expand_binop (mode,
  1159. unsignedp ? umod_optab : smod_optab,
  1160. adjusted_op0, op1, target,
  1161. unsignedp, OPTAB_WIDEN);
  1162. if (temp != 0)
  1163. return temp;
  1164. /* No luck there.
  1165. Can we do remainder and divide at once without a library call? */
  1166. temp = gen_reg_rtx (mode);
  1167. if (expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  1168. adjusted_op0, op1,
  1169. 0, temp, unsignedp))
  1170. return temp;
  1171. temp = 0;
  1172. }
  1173. }
  1174. /* Produce the quotient. */
  1175. if (log >= 0)
  1176. temp = expand_shift (RSHIFT_EXPR, mode, adjusted_op0,
  1177. build_int_2 (exact_log2 (INTVAL (op1)), 0),
  1178. target, unsignedp);
  1179. else if (rem_flag && !mod_insn_no_good)
  1180. /* If producing quotient in order to subtract for remainder,
  1181. and a remainder subroutine would be ok,
  1182. don't use a divide subroutine. */
  1183. temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  1184. adjusted_op0, op1, target,
  1185. unsignedp, OPTAB_WIDEN);
  1186. else
  1187. temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  1188. adjusted_op0, op1, target,
  1189. unsignedp, OPTAB_LIB_WIDEN);
  1190. /* If we really want the remainder, get it by subtraction. */
  1191. if (rem_flag)
  1192. {
  1193. if (temp == 0)
  1194. {
  1195. /* No divide instruction either. Use library for remainder. */
  1196. temp = expand_binop (mode,
  1197. unsignedp ? umod_optab : smod_optab,
  1198. op0, op1, target,
  1199. unsignedp, OPTAB_LIB_WIDEN);
  1200. }
  1201. else
  1202. {
  1203. /* We divided. Now finish doing X - Y * (X / Y). */
  1204. temp = expand_mult (mode, temp, op1, temp, unsignedp);
  1205. if (! temp) abort ();
  1206. temp = expand_binop (mode, sub_optab, op0,
  1207. temp, target, unsignedp, OPTAB_LIB_WIDEN);
  1208. }
  1209. }
  1210. if (temp == 0)
  1211. abort ();
  1212. return temp;
  1213. }