fold-const.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*@@ Fix lossage on folding division of big integers. */
  2. /*@@ This file should be rewritten to use an arbitary precision
  3. @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
  4. @@ Perhaps the routines could also be used for bc/dc, and made a lib.
  5. @@ The routines that translate from the ap rep should
  6. @@ warn if precision et. al. is lost.
  7. @@ This would also make life easier when this technology is used
  8. @@ for cross-compilers. */
  9. /* Fold a constant sub-tree into a single node for C-compiler
  10. Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  11. This file is part of GNU CC.
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY. No author or distributor
  14. accepts responsibility to anyone for the consequences of using it
  15. or for whether it serves any particular purpose or works at all,
  16. unless he says so in writing. Refer to the GNU CC General Public
  17. License for full details.
  18. Everyone is granted permission to copy, modify and redistribute
  19. GNU CC, but only under the conditions described in the
  20. GNU CC General Public License. A copy of this license is
  21. supposed to have been given to you along with GNU CC so you
  22. can know your rights and responsibilities. It should be in a
  23. file named COPYING. Among other things, the copyright notice
  24. and this notice must be preserved on all copies. */
  25. /* There are only two entry points in this file:
  26. fold and combine.
  27. fold takes a tree as argument and returns a simplified tree.
  28. combine takes a tree code for an arithmetic operation
  29. and two operands that are trees for constant values
  30. and returns the result of the specified operation on those values,
  31. also as a tree. */
  32. #include <stdio.h>
  33. #include "config.h"
  34. #include "tree.h"
  35. static void lshift_double ();
  36. static void rshift_double ();
  37. static void lrotate_double ();
  38. static void rrotate_double ();
  39. /* To do constant folding on INTEGER_CST nodes requires 64-bit arithmetic.
  40. We do that by representing the 64-bit integer as 8 shorts,
  41. with only 8 bits stored in each short, as a positive number. */
  42. /* Unpack a 64-bit integer into 8 shorts.
  43. LOW and HI are the integer, as two `int' pieces.
  44. SHORTS points to the array of shorts. */
  45. static void
  46. encode (shorts, low, hi)
  47. short *shorts;
  48. int low, hi;
  49. {
  50. shorts[0] = low & 0xff;
  51. shorts[1] = (low >> 8) & 0xff;
  52. shorts[2] = (low >> 16) & 0xff;
  53. shorts[3] = (low >> 24) & 0xff;
  54. shorts[4] = hi & 0xff;
  55. shorts[5] = (hi >> 8) & 0xff;
  56. shorts[6] = (hi >> 16) & 0xff;
  57. shorts[7] = (hi >> 24) & 0xff;
  58. }
  59. /* Pack an array of 8 shorts into a 64-bit integer.
  60. SHORTS points to the array of shorts.
  61. The integer is stored into *LOW and *HI as two `int' pieces. */
  62. static void
  63. decode (shorts, low, hi)
  64. short *shorts;
  65. int *low, *hi;
  66. {
  67. *low = (shorts[3] << 24) | (shorts[2] << 16) | (shorts[1] << 8) | shorts[0];
  68. *hi = (shorts[7] << 24) | (shorts[6] << 16) | (shorts[5] << 8) | shorts[4];
  69. }
  70. /* Make the integer constant T valid for its type
  71. by setting to 0 or 1 all the bits in the constant
  72. that don't belong in the type. */
  73. static void
  74. force_fit_type (t)
  75. tree t;
  76. {
  77. register int prec = TYPE_PRECISION (TREE_TYPE (t));
  78. if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  79. prec = BITS_PER_WORD;
  80. /* First clear all bits that are beyond the type's precision. */
  81. if (prec > HOST_BITS_PER_INT)
  82. {
  83. TREE_INT_CST_HIGH (t)
  84. &= ~((-1) << (prec - HOST_BITS_PER_INT));
  85. }
  86. else
  87. {
  88. TREE_INT_CST_HIGH (t) = 0;
  89. if (prec < HOST_BITS_PER_INT)
  90. TREE_INT_CST_LOW (t)
  91. &= ~((-1) << prec);
  92. }
  93. /* If it's a signed type and value's sign bit is set, extend the sign. */
  94. if (! TREE_UNSIGNED (TREE_TYPE (t))
  95. && (prec > HOST_BITS_PER_INT
  96. ? TREE_INT_CST_HIGH (t) & (1 << (prec - HOST_BITS_PER_INT - 1))
  97. : TREE_INT_CST_LOW (t) & (1 << (prec - 1))))
  98. {
  99. /* Value is negative:
  100. set to 1 all the bits that are outside this type's precision. */
  101. if (prec > HOST_BITS_PER_INT)
  102. {
  103. TREE_INT_CST_HIGH (t)
  104. |= ((-1) << (prec - HOST_BITS_PER_INT));
  105. }
  106. else
  107. {
  108. TREE_INT_CST_HIGH (t) = -1;
  109. if (prec < HOST_BITS_PER_INT)
  110. TREE_INT_CST_LOW (t)
  111. |= ((-1) << prec);
  112. }
  113. }
  114. }
  115. /* Add two 64-bit integers with 64-bit result.
  116. Each argument is given as two `int' pieces.
  117. One argument is L1 and H1; the other, L2 and H2.
  118. The value is stored as two `int' pieces in *LV and *HV.
  119. We use the 8-shorts representation internally. */
  120. static void
  121. add_double (l1, h1, l2, h2, lv, hv)
  122. int l1, h1, l2, h2;
  123. int *lv, *hv;
  124. {
  125. short arg1[8];
  126. short arg2[8];
  127. register int carry = 0;
  128. register int i;
  129. encode (arg1, l1, h1);
  130. encode (arg2, l2, h2);
  131. for (i = 0; i < 8; i++)
  132. {
  133. carry += arg1[i] + arg2[i];
  134. arg1[i] = carry & 0xff;
  135. carry >>= 8;
  136. }
  137. decode (arg1, lv, hv);
  138. }
  139. /* Negate a 64-bit integers with 64-bit result.
  140. The argument is given as two `int' pieces in L1 and H1.
  141. The value is stored as two `int' pieces in *LV and *HV.
  142. We use the 8-shorts representation internally. */
  143. static void
  144. neg_double (l1, h1, lv, hv)
  145. int l1, h1;
  146. int *lv, *hv;
  147. {
  148. if (l1 == 0)
  149. {
  150. *lv = 0;
  151. *hv = - h1;
  152. }
  153. else
  154. {
  155. *lv = - l1;
  156. *hv = ~ h1;
  157. }
  158. }
  159. /* Multiply two 64-bit integers with 64-bit result.
  160. Each argument is given as two `int' pieces.
  161. One argument is L1 and H1; the other, L2 and H2.
  162. The value is stored as two `int' pieces in *LV and *HV.
  163. We use the 8-shorts representation internally. */
  164. static void
  165. mul_double (l1, h1, l2, h2, lv, hv)
  166. int l1, h1, l2, h2;
  167. int *lv, *hv;
  168. {
  169. short arg1[8];
  170. short arg2[8];
  171. short prod[16];
  172. register int carry = 0;
  173. register int i, j, k;
  174. encode (arg1, l1, h1);
  175. encode (arg2, l2, h2);
  176. bzero (prod, sizeof prod);
  177. for (i = 0; i < 8; i++)
  178. for (j = 0; j < 8; j++)
  179. {
  180. k = i + j;
  181. carry = arg1[i] * arg2[j];
  182. while (carry)
  183. {
  184. carry += prod[k];
  185. prod[k] = carry & 0xff;
  186. carry >>= 8;
  187. k++;
  188. }
  189. }
  190. decode (prod, lv, hv); /* @@decode ignores prod[8] -> prod[15] */
  191. }
  192. /* Shift the 64-bit integer in L1, H1 left by COUNT places
  193. keeping only PREC bits of result.
  194. Shift right if COUNT is negative.
  195. ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  196. Store the value as two `int' pieces in *LV and *HV. */
  197. static void
  198. lshift_double (l1, h1, count, prec, lv, hv, arith)
  199. int l1, h1, count, prec;
  200. int *lv, *hv;
  201. int arith;
  202. {
  203. short arg1[8];
  204. register int i;
  205. register int carry;
  206. if (count < 0)
  207. {
  208. rshift_double (l1, h1, - count, prec, lv, hv, arith);
  209. return;
  210. }
  211. encode (arg1, l1, h1);
  212. if (prec < HOST_BITS_PER_INT)
  213. count &= (1 << prec) - 1;
  214. while (count > 0)
  215. {
  216. carry = 0;
  217. for (i = 0; i < 8; i++)
  218. {
  219. carry += arg1[i] << 1;
  220. arg1[i] = carry & 0xff;
  221. carry >>= 8;
  222. }
  223. count--;
  224. }
  225. decode (arg1, lv, hv);
  226. }
  227. /* Shift the 64-bit integer in L1, H1 right by COUNT places
  228. keeping only PREC bits of result. COUNT must be positive.
  229. ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  230. Store the value as two `int' pieces in *LV and *HV. */
  231. static void
  232. rshift_double (l1, h1, count, prec, lv, hv, arith)
  233. int l1, h1, count, prec;
  234. int *lv, *hv;
  235. int arith;
  236. {
  237. short arg1[8];
  238. register int i;
  239. register int carry;
  240. encode (arg1, l1, h1);
  241. if (prec < HOST_BITS_PER_INT)
  242. count &= (1 << prec) - 1;
  243. carry = arith && arg1[7] >> 7;
  244. while (count > 0)
  245. {
  246. for (i = 7; i >= 0; i--)
  247. {
  248. carry <<= 8;
  249. carry += arg1[i];
  250. arg1[i] = (carry >> 1) & 0xff;
  251. }
  252. count--;
  253. }
  254. decode (arg1, lv, hv);
  255. }
  256. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  257. keeping only PREC bits of result.
  258. Rotate right if COUNT is negative.
  259. Store the value as two `int' pieces in *LV and *HV. */
  260. static void
  261. lrotate_double (l1, h1, count, prec, lv, hv)
  262. int l1, h1, count, prec;
  263. int *lv, *hv;
  264. {
  265. short arg1[8];
  266. register int i;
  267. register int carry;
  268. if (count < 0)
  269. {
  270. rrotate_double (l1, h1, - count, prec, lv, hv);
  271. return;
  272. }
  273. encode (arg1, l1, h1);
  274. if (prec < HOST_BITS_PER_INT)
  275. count &= (1 << prec) - 1;
  276. carry = arg1[7] >> 7;
  277. while (count > 0)
  278. {
  279. for (i = 0; i < 8; i++)
  280. {
  281. carry += arg1[i] << 1;
  282. arg1[i] = carry & 0xff;
  283. carry >>= 8;
  284. }
  285. count--;
  286. }
  287. decode (arg1, lv, hv);
  288. }
  289. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  290. keeping only PREC bits of result. COUNT must be positive.
  291. Store the value as two `int' pieces in *LV and *HV. */
  292. static void
  293. rrotate_double (l1, h1, count, prec, lv, hv)
  294. int l1, h1, count, prec;
  295. int *lv, *hv;
  296. {
  297. short arg1[8];
  298. register int i;
  299. register int carry;
  300. encode (arg1, l1, h1);
  301. if (prec < HOST_BITS_PER_INT)
  302. count &= (1 << prec) - 1;
  303. carry = arg1[0] & 1;
  304. while (count > 0)
  305. {
  306. for (i = 7; i >= 0; i--)
  307. {
  308. carry <<= 8;
  309. carry += arg1[i];
  310. arg1[i] = (carry >> 1) & 0xff;
  311. }
  312. count--;
  313. }
  314. decode (arg1, lv, hv);
  315. }
  316. /* Divide 64 bit integer LNUM, HNUM by 64 bit integer LDEN, HDEN
  317. for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  318. CODE is a tree code for a kind of division, one of
  319. TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR and ROUND_DIV_EXPR.
  320. It controls how the quotient is rounded to a integer.
  321. UNS nonzero says do unsigned division. */
  322. static void
  323. div_and_round_double (code, uns,
  324. lnum_orig, hnum_orig, lden_orig, hden_orig,
  325. lquo, hquo, lrem, hrem)
  326. enum tree_code code;
  327. int uns;
  328. int lnum_orig, hnum_orig; /* num == numerator == dividend */
  329. int lden_orig, hden_orig; /* den == denominator == divisor */
  330. int *lquo, *hquo, *lrem, *hrem;
  331. {
  332. int quo_neg = 0;
  333. short num[9], den[8], quo[8]; /* extra element for scaling. */
  334. register int i, j, work;
  335. register int carry = 0;
  336. int lnum = lnum_orig, hnum = hnum_orig;
  337. int lden = lden_orig, hden = hden_orig;
  338. if ((hden == 0) && (lden == 0)) {
  339. *hquo = *lquo = *hrem = *lrem = 0;
  340. error
  341. ("divide by 0 in constant folding - quotient and remainder set to 0.");
  342. return;
  343. }
  344. /* calculate quotient sign and convert operands to unsigned. */
  345. if (!uns)
  346. {
  347. if (hden < 0)
  348. {
  349. quo_neg = ~ quo_neg;
  350. neg_double (lden, hden, &lden, &hden);
  351. }
  352. if (hnum < 0)
  353. {
  354. quo_neg = ~ quo_neg;
  355. neg_double (lnum, hnum, &lnum, &hnum);
  356. }
  357. }
  358. if (hnum == 0 && hden == 0)
  359. { /* single precision */
  360. *hquo = *hrem = 0;
  361. *lquo = (unsigned) lnum / lden; /* rounds toward zero since positive args */
  362. goto finish_up;
  363. }
  364. if (hnum == 0)
  365. { /* trivial case: dividend < divisor */
  366. /* hden != 0 already checked. */
  367. *hquo = *lquo = 0;
  368. *hrem = hnum;
  369. *lrem = lnum;
  370. goto finish_up;
  371. }
  372. bzero (quo, sizeof quo);
  373. bzero (num, sizeof num); /* to zero 9th element */
  374. bzero (den, sizeof den);
  375. encode (num, lnum, hnum);
  376. encode (den, lden, hden);
  377. if (hden == 0)
  378. { /* simpler algorithm */
  379. /* hnum != 0 already checked. */
  380. for (i = 7; i >= 0; i--)
  381. {
  382. work = num[i] + (carry << 8);
  383. quo[i] = work / lden;
  384. carry = work % lden;
  385. }
  386. }
  387. else { /* full double precision,
  388. with thanks to Don Knuth's
  389. "Semi-Numericial Algorithms". */
  390. #define BASE 256
  391. int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
  392. /* Find the highest non-zero divisor digit. */
  393. for (i = 7; ; i--)
  394. if (den[i] != 0) {
  395. den_hi_sig = i;
  396. break;
  397. }
  398. for (i = 7; ; i--)
  399. if (num[i] != 0) {
  400. num_hi_sig = i;
  401. break;
  402. }
  403. quo_hi_sig = num_hi_sig - den_hi_sig + 1;
  404. /* Insure that the first digit of the divisor is at least BASE/2.
  405. This is required by the quotient digit estimation algorithm. */
  406. scale = BASE / (den[den_hi_sig] + 1);
  407. if (scale > 1) { /* scale divisor and dividend */
  408. carry = 0;
  409. for (i = 0; i <= 8; i++) {
  410. work = (num[i] * scale) + carry;
  411. num[i] = work & 0xff;
  412. carry = work >> 8;
  413. if (num[i] != 0) num_hi_sig = i;
  414. }
  415. carry = 0;
  416. for (i = 0; i <= 7; i++) {
  417. work = (den[i] * scale) + carry;
  418. den[i] = work & 0xff;
  419. carry = work >> 8;
  420. if (den[i] != 0) den_hi_sig = i;
  421. }
  422. }
  423. /* Main loop */
  424. for (i = quo_hi_sig; i > 0; i--) {
  425. /* quess the next quotient digit, quo_est, by dividing the first
  426. two remaining dividend digits by the high order quotient digit.
  427. quo_est is never low and is at most 2 high. */
  428. int num_hi; /* index of highest remaining dividend digit */
  429. num_hi = i + den_hi_sig;
  430. work = (num[num_hi] * BASE) + (num_hi ? 0 : num[num_hi - 1]);
  431. if (num[num_hi] != den[den_hi_sig]) {
  432. quo_est = work / den[den_hi_sig];
  433. }
  434. else {
  435. quo_est = BASE - 1;
  436. }
  437. /* refine quo_est so it's usually correct, and at most one high. */
  438. while ((den[den_hi_sig - 1] * quo_est)
  439. > (((work - (quo_est * den[den_hi_sig])) * BASE)
  440. + ((num_hi - 1) ? 0 : num[num_hi - 2]))) {
  441. quo_est--;
  442. }
  443. /* try quo_est as the quotient digit, by multiplying the
  444. divisor by quo_est and subtracting from the remaining dividend. */
  445. carry = 0;
  446. for (j = 0; j <= den_hi_sig; j++) {
  447. int digit;
  448. work = num[i + j] - (quo_est * den[j]) + carry;
  449. digit = work & 0xff;
  450. carry = work >> 8;
  451. if (digit < 0) {
  452. digit += BASE;
  453. carry--;
  454. }
  455. num[i + j] = digit;
  456. }
  457. /* if quo_est was high by one, then num[i] went negative and
  458. we need to correct things. */
  459. if (num[num_hi] < 0) {
  460. quo_est--;
  461. carry = 0; /* add divisor back in */
  462. for (j = 0; j <= den_hi_sig; j++) {
  463. work = num[i + j] + den[j] + carry;
  464. if (work > BASE) {
  465. work -= BASE;
  466. carry = 1;
  467. }
  468. else {
  469. carry = 0;
  470. }
  471. num[i + j] = work;
  472. }
  473. num [num_hi] += carry;
  474. }
  475. /* store the quotient digit. */
  476. quo[i - 1] = quo_est;
  477. }
  478. }
  479. decode (quo, lquo, hquo);
  480. finish_up:
  481. /* if result is negative, make it so. */
  482. if (quo_neg)
  483. neg_double (*lquo, *hquo, lquo, hquo);
  484. /* compute trial remainder: rem = num - (quo * den) */
  485. mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  486. neg_double (*lrem, *hrem, lrem, hrem);
  487. add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  488. switch (code)
  489. {
  490. case TRUNC_DIV_EXPR:
  491. case TRUNC_MOD_EXPR: /* round toward zero */
  492. return;
  493. case FLOOR_DIV_EXPR:
  494. case FLOOR_MOD_EXPR: /* round toward negative infinity */
  495. if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
  496. {
  497. /* quo = quo - 1; */
  498. add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  499. }
  500. else return;
  501. break;
  502. case CEIL_DIV_EXPR:
  503. case CEIL_MOD_EXPR: /* round toward positive infinity */
  504. if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
  505. {
  506. add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  507. }
  508. else return;
  509. break;
  510. case ROUND_DIV_EXPR:
  511. case ROUND_MOD_EXPR: /* round to closest integer */
  512. {
  513. int labs_rem = *lrem, habs_rem = *hrem;
  514. int labs_den = lden, habs_den = hden, ltwice, htwice;
  515. /* get absolute values */
  516. if (*hrem < 0) neg_double(*lrem, *hrem, &labs_rem, &habs_rem);
  517. if (hden < 0) neg_double(lden, hden, &labs_den, &habs_den);
  518. /* if (2 * abs (lrem) >= abs (lden)) */
  519. mul_double(2, 0, labs_rem, habs_rem, &ltwice, &htwice);
  520. if (((unsigned) habs_den < (unsigned) htwice)
  521. || (((unsigned) habs_den == (unsigned) htwice)
  522. && ((unsigned) labs_den < (unsigned) ltwice)))
  523. {
  524. if (*hquo < 0)
  525. /* quo = quo - 1; */
  526. add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  527. else
  528. /* quo = quo + 1; */
  529. add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  530. }
  531. else return;
  532. }
  533. break;
  534. default:
  535. abort ();
  536. }
  537. /* compute true remainder: rem = num - (quo * den) */
  538. mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  539. neg_double (*lrem, *hrem, lrem, hrem);
  540. add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  541. }
  542. /* Split a tree IN into a constant and a variable part
  543. that could be combined with CODE to make IN.
  544. CODE must be a commutative arithmetic operation.
  545. Store the constant part into *CONP and the variable in &VARP.
  546. Return 1 if this was done; zero means the tree IN did not decompose
  547. this way.
  548. If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
  549. Therefore, we must tell the caller whether the variable part
  550. was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
  551. The value stored is the coefficient for the variable term.
  552. The constant term we return should always be added;
  553. we negate it if necessary. */
  554. static int
  555. split_tree (in, code, varp, conp, varsignp)
  556. tree in;
  557. enum tree_code code;
  558. tree *varp, *conp;
  559. int *varsignp;
  560. {
  561. register tree outtype = TREE_TYPE (in);
  562. *varp = 0;
  563. *conp = 0;
  564. /* Strip any conversions that don't change the machine mode. */
  565. while ((TREE_CODE (in) == NOP_EXPR
  566. || TREE_CODE (in) == CONVERT_EXPR)
  567. && (TYPE_MODE (TREE_TYPE (in))
  568. == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
  569. in = TREE_OPERAND (in, 0);
  570. if (TREE_CODE (in) == code
  571. || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE
  572. /* We can associate addition and subtraction together
  573. (even though the C standard doesn't say so)
  574. for integers because the value is not affected.
  575. For reals, the value might be affected, so we can't. */
  576. &&
  577. ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
  578. || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
  579. {
  580. enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
  581. if (code == INTEGER_CST || code == REAL_CST)
  582. {
  583. *conp = TREE_OPERAND (in, 0);
  584. *varp = TREE_OPERAND (in, 1);
  585. if (TREE_TYPE (*varp) != outtype)
  586. *varp = convert (outtype, *varp);
  587. *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  588. return 1;
  589. }
  590. if (TREE_LITERAL (TREE_OPERAND (in, 1)))
  591. {
  592. *conp = TREE_OPERAND (in, 1);
  593. *varp = TREE_OPERAND (in, 0);
  594. *varsignp = 1;
  595. if (TREE_TYPE (*varp) != outtype)
  596. *varp = convert (outtype, *varp);
  597. if (TREE_CODE (in) == MINUS_EXPR)
  598. {
  599. /* If operation is subtraction and constant is second,
  600. must negate it to get an additive constant.
  601. And this cannot be done unless it is a manifest constant.
  602. It could also be the address of a static variable.
  603. We cannot negate that, so give up. */
  604. if (TREE_CODE (*conp) == INTEGER_CST
  605. || TREE_CODE (*conp) == REAL_CST)
  606. *conp = combine (MINUS_EXPR, integer_zero_node, *conp);
  607. else
  608. return 0;
  609. }
  610. return 1;
  611. }
  612. if (TREE_LITERAL (TREE_OPERAND (in, 0)))
  613. {
  614. *conp = TREE_OPERAND (in, 0);
  615. *varp = TREE_OPERAND (in, 1);
  616. if (TREE_TYPE (*varp) != outtype)
  617. *varp = convert (outtype, *varp);
  618. *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  619. return 1;
  620. }
  621. }
  622. return 0;
  623. }
  624. /* Combine two constants NUM and ARG2 under operation CODE
  625. to produce a new constant.
  626. We assume ARG1 and ARG2 have the same data type,
  627. or at least are the same kind of constant and the same machine mode. */
  628. tree
  629. combine (code, arg1, arg2)
  630. enum tree_code code;
  631. register tree arg1, arg2;
  632. {
  633. if (TREE_CODE (arg1) == INTEGER_CST)
  634. {
  635. register int int1l = TREE_INT_CST_LOW (arg1);
  636. register int int1h = TREE_INT_CST_HIGH (arg1);
  637. int int2l = TREE_INT_CST_LOW (arg2);
  638. int int2h = TREE_INT_CST_HIGH (arg2);
  639. int low, hi;
  640. int garbagel, garbageh;
  641. register tree t;
  642. int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
  643. switch (code)
  644. {
  645. case BIT_IOR_EXPR:
  646. t = build_int_2 (int1l | int2l, int1h | int2h);
  647. break;
  648. case BIT_XOR_EXPR:
  649. t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
  650. break;
  651. case BIT_AND_EXPR:
  652. t = build_int_2 (int1l & int2l, int1h & int2h);
  653. break;
  654. case BIT_ANDTC_EXPR:
  655. t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
  656. break;
  657. case RSHIFT_EXPR:
  658. int2l = - int2l;
  659. case LSHIFT_EXPR:
  660. lshift_double (int1l, int1h, int2l,
  661. TYPE_PRECISION (TREE_TYPE (arg1)),
  662. &low, &hi,
  663. !uns);
  664. t = build_int_2 (low, hi);
  665. break;
  666. case RROTATE_EXPR:
  667. int2l = - int2l;
  668. case LROTATE_EXPR:
  669. lrotate_double (int1l, int1h, int2l,
  670. TYPE_PRECISION (TREE_TYPE (arg1)),
  671. &low, &hi);
  672. t = build_int_2 (low, hi);
  673. break;
  674. case PLUS_EXPR:
  675. add_double (int1l, int1h, int2l, int2h, &low, &hi);
  676. t = build_int_2 (low, hi);
  677. break;
  678. case MINUS_EXPR:
  679. neg_double (int2l, int2h, &int2l, &int2h);
  680. add_double (int1l, int1h, int2l, int2h, &low, &hi);
  681. t = build_int_2 (low, hi);
  682. break;
  683. case MULT_EXPR:
  684. mul_double (int1l, int1h, int2l, int2h, &low, &hi);
  685. t = build_int_2 (low, hi);
  686. break;
  687. case TRUNC_DIV_EXPR: case ROUND_DIV_EXPR:
  688. case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
  689. div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  690. &low, &hi, &garbagel, &garbageh);
  691. t = build_int_2 (low, hi);
  692. break;
  693. case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR:
  694. case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
  695. div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  696. &garbagel, &garbageh, &low, &hi);
  697. t = build_int_2 (low, hi);
  698. break;
  699. case MIN_EXPR:
  700. case MAX_EXPR:
  701. if (uns)
  702. {
  703. low = (((unsigned) int1h < (unsigned) int2h)
  704. || (((unsigned) int1h == (unsigned) int2h)
  705. && ((unsigned) int1l < (unsigned) int2l)));
  706. }
  707. else
  708. {
  709. low = ((int1h < int2h)
  710. || ((int1h == int2h)
  711. && ((unsigned) int1l < (unsigned) int2l)));
  712. }
  713. if (low == (code == MIN_EXPR))
  714. t = build_int_2 (int1l, int1h);
  715. else
  716. t = build_int_2 (int2l, int2h);
  717. break;
  718. default:
  719. abort ();
  720. }
  721. TREE_TYPE (t) = TREE_TYPE (arg1);
  722. force_fit_type (t);
  723. return t;
  724. }
  725. if (TREE_CODE (arg1) == REAL_CST)
  726. {
  727. register double d1 = TREE_REAL_CST (arg1);
  728. register double d2 = TREE_REAL_CST (arg2);
  729. register tree t;
  730. switch (code)
  731. {
  732. case PLUS_EXPR:
  733. t = build_real (d1 + d2);
  734. break;
  735. case MINUS_EXPR:
  736. t = build_real (d1 - d2);
  737. break;
  738. case MULT_EXPR:
  739. t = build_real (d1 * d2);
  740. break;
  741. case RDIV_EXPR:
  742. if (d2 == 0)
  743. return 0;
  744. t = build_real (d1 / d2);
  745. break;
  746. case MIN_EXPR:
  747. if (d1 < d2)
  748. t = build_real (d1);
  749. t = build_real (d2);
  750. break;
  751. case MAX_EXPR:
  752. if (d1 > d2)
  753. t = build_real (d1);
  754. t = build_real (d2);
  755. break;
  756. default:
  757. abort ();
  758. }
  759. TREE_TYPE (t) = TREE_TYPE (arg1);
  760. return t;
  761. }
  762. if (TREE_CODE (arg1) == COMPLEX_CST)
  763. {
  764. register tree r1 = TREE_REALPART (arg1);
  765. register tree i1 = TREE_IMAGPART (arg1);
  766. register tree r2 = TREE_REALPART (arg2);
  767. register tree i2 = TREE_IMAGPART (arg2);
  768. register tree t;
  769. switch (code)
  770. {
  771. case PLUS_EXPR:
  772. t = build_complex (combine (PLUS_EXPR, r1, r2),
  773. combine (PLUS_EXPR, i1, i2));
  774. break;
  775. case MINUS_EXPR:
  776. t = build_complex (combine (MINUS_EXPR, r1, r2),
  777. combine (MINUS_EXPR, i1, i2));
  778. break;
  779. case MULT_EXPR:
  780. t = build_complex (combine (MINUS_EXPR,
  781. combine (MULT_EXPR, r1, r2),
  782. combine (MULT_EXPR, i1, i2)),
  783. combine (PLUS_EXPR,
  784. combine (MULT_EXPR, r1, i2),
  785. combine (MULT_EXPR, i1, r2)));
  786. break;
  787. case RDIV_EXPR:
  788. {
  789. register tree magsquared
  790. = combine (PLUS_EXPR,
  791. combine (MULT_EXPR, r2, r2),
  792. combine (MULT_EXPR, i2, i2));
  793. t = build_complex (combine (RDIV_EXPR,
  794. combine (PLUS_EXPR,
  795. combine (MULT_EXPR, r1, r2),
  796. combine (MULT_EXPR, i1, i2)),
  797. magsquared),
  798. combine (RDIV_EXPR,
  799. combine (MINUS_EXPR,
  800. combine (MULT_EXPR, i1, r2),
  801. combine (MULT_EXPR, r1, i2)),
  802. magsquared));
  803. }
  804. break;
  805. default:
  806. abort ();
  807. }
  808. TREE_TYPE (t) = TREE_TYPE (arg1);
  809. return t;
  810. }
  811. return 0;
  812. }
  813. /* Given T, a tree representing type conversion of a constant,
  814. return a constant tree representing the result of conversion. */
  815. static tree
  816. fold_convert (t)
  817. register tree t;
  818. {
  819. register tree arg1 = TREE_OPERAND (t, 0);
  820. register tree type = TREE_TYPE (t);
  821. if (TREE_CODE (type) == POINTER_TYPE
  822. || TREE_CODE (type) == INTEGER_TYPE
  823. || TREE_CODE (type) == ENUMERAL_TYPE)
  824. {
  825. if (TREE_CODE (arg1) == INTEGER_CST)
  826. {
  827. /* Given an integer constant, make new constant with new type,
  828. appropriately sign-extended or truncated. */
  829. register int inprec;
  830. register int outprec;
  831. if (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
  832. inprec = BITS_PER_WORD;
  833. else
  834. inprec = TYPE_PRECISION (TREE_TYPE (arg1));
  835. if (TREE_CODE (type) == POINTER_TYPE)
  836. outprec = BITS_PER_WORD;
  837. else
  838. outprec = TYPE_PRECISION (type);
  839. t = build_int_2 (TREE_INT_CST_LOW (arg1),
  840. TREE_INT_CST_HIGH (arg1));
  841. TREE_TYPE (t) = type;
  842. force_fit_type (t);
  843. }
  844. else if (TREE_CODE (arg1) == REAL_CST)
  845. t = build_int_2 ((int) TREE_REAL_CST (arg1),
  846. (int) (TREE_REAL_CST (arg1) / 0x10000 / 0x10000));
  847. }
  848. else if (TREE_CODE (type) == REAL_TYPE)
  849. {
  850. if (TREE_CODE (arg1) == INTEGER_CST)
  851. t = build_real_from_int_cst (arg1);
  852. else if (TREE_CODE (arg1) == REAL_CST)
  853. t = build_real (TREE_REAL_CST (arg1));
  854. }
  855. TREE_TYPE (t) = type;
  856. TREE_LITERAL (t) = 1;
  857. return t;
  858. }
  859. /* Return nonzero if two constants (that are not manifest constants)
  860. are necessarily equal. It detects only the easiest, common case of
  861. equality. */
  862. static int
  863. operand_equal_p (arg0, arg1)
  864. tree arg0, arg1;
  865. {
  866. while ((TREE_CODE (arg0) == NOP_EXPR
  867. || TREE_CODE (arg0) == CONVERT_EXPR)
  868. && TYPE_MODE (TREE_TYPE (arg0)) == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  869. arg0 = TREE_OPERAND (arg0, 0);
  870. while ((TREE_CODE (arg1) == NOP_EXPR
  871. || TREE_CODE (arg1) == CONVERT_EXPR)
  872. && TYPE_MODE (TREE_TYPE (arg1)) == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  873. arg1 = TREE_OPERAND (arg1, 0);
  874. if (TREE_CODE (arg0) == TREE_CODE (arg1)
  875. && TREE_CODE (arg0) == ADDR_EXPR
  876. && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
  877. return 1;
  878. return 0;
  879. }
  880. /* Perform constant folding and related simplification of EXPR.
  881. The related simplifications include x*1 => x, x*0 => 0, etc.,
  882. and application of the associative law.
  883. NOP_EXPR conversions may be removed freely (as long as we
  884. are careful not to change the C type of the overall expression)
  885. We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
  886. but we can constant-fold them if they have constant operands. */
  887. tree
  888. fold (expr)
  889. tree expr;
  890. {
  891. register tree t = expr;
  892. register tree arg0, arg1;
  893. register enum tree_code code = TREE_CODE (t);
  894. register int kind;
  895. /* WINS will be nonzero when the switch is done
  896. if all operands are constant.
  897. LOSES will be nonzero when the switch is done
  898. if any operand is volatile.
  899. This inhibits optimizations such as (foo () * 0) => 0.
  900. But identity-element optimizations such as
  901. (foo () * 1) => (foo ()) can be done even if LOSES is set. */
  902. int wins = 1;
  903. int loses = 0;
  904. /* Return right away if already constant. */
  905. if (TREE_LITERAL (t))
  906. {
  907. if (code == CONST_DECL)
  908. return DECL_INITIAL (t);
  909. return t;
  910. }
  911. kind = *tree_code_type[(int) code];
  912. if (kind == 'e' || kind == 'r')
  913. {
  914. register int len = tree_code_length[(int) code];
  915. register int i;
  916. for (i = 0; i < len; i++)
  917. {
  918. if (TREE_OPERAND (t, i) == 0)
  919. continue; /* Valid for CALL_EXPR, at least. */
  920. if (TREE_CODE (TREE_OPERAND (t, i)) != INTEGER_CST
  921. && TREE_CODE (TREE_OPERAND (t, i)) != REAL_CST)
  922. /* Note that TREE_LITERAL isn't enough:
  923. static var addresses are constant but we can't
  924. do arithmetic on them. */
  925. wins = 0;
  926. if (TREE_VOLATILE (TREE_OPERAND (t, i)))
  927. loses = 1;
  928. }
  929. arg0 = TREE_OPERAND (t, 0);
  930. if (len > 1)
  931. arg1 = TREE_OPERAND (t, 1);
  932. }
  933. /* Now WINS and LOSES are set as described above,
  934. ARG0 is the first operand of EXPR,
  935. and ARG1 is the second operand (if it has more than one operand). */
  936. switch (code)
  937. {
  938. case INTEGER_CST:
  939. case REAL_CST:
  940. case STRING_CST:
  941. case COMPLEX_CST:
  942. case CONSTRUCTOR:
  943. return t;
  944. case CONST_DECL:
  945. return fold (DECL_INITIAL (t));
  946. case NOP_EXPR:
  947. case FLOAT_EXPR:
  948. case CONVERT_EXPR:
  949. case FIX_TRUNC_EXPR:
  950. /* Other kinds of FIX are not handled properly by fold_convert. */
  951. if (!wins)
  952. {
  953. TREE_LITERAL (t) = TREE_LITERAL (arg0);
  954. return t;
  955. }
  956. return fold_convert (t);
  957. case RANGE_EXPR:
  958. TREE_LITERAL (t) = wins;
  959. return t;
  960. case NEGATE_EXPR:
  961. if (wins)
  962. {
  963. if (TREE_CODE (arg0) == INTEGER_CST)
  964. {
  965. if (TREE_INT_CST_LOW (arg0) == 0)
  966. t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  967. else
  968. t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  969. ~ TREE_INT_CST_HIGH (arg0));
  970. force_fit_type (t);
  971. }
  972. else if (TREE_CODE (arg0) == REAL_CST)
  973. t = build_real (- TREE_REAL_CST (arg0));
  974. TREE_TYPE (t) = TREE_TYPE (expr);
  975. }
  976. return t;
  977. case ABS_EXPR:
  978. if (wins)
  979. {
  980. if (TREE_CODE (arg0) == INTEGER_CST)
  981. {
  982. if (! TREE_UNSIGNED (TREE_TYPE (expr))
  983. || TREE_INT_CST_HIGH (arg0) < 0)
  984. {
  985. if (TREE_INT_CST_LOW (arg0) == 0)
  986. t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  987. else
  988. t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  989. ~ TREE_INT_CST_HIGH (arg0));
  990. }
  991. }
  992. else if (TREE_CODE (arg0) == REAL_CST)
  993. {
  994. if (TREE_REAL_CST (arg0) < 0)
  995. t = build_real (- TREE_REAL_CST (arg0));
  996. }
  997. TREE_TYPE (t) = TREE_TYPE (expr);
  998. }
  999. return t;
  1000. case BIT_NOT_EXPR:
  1001. if (wins)
  1002. {
  1003. if (TREE_CODE (arg0) == INTEGER_CST)
  1004. t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
  1005. ~ TREE_INT_CST_HIGH (arg0));
  1006. TREE_TYPE (t) = TREE_TYPE (expr);
  1007. force_fit_type (t);
  1008. }
  1009. return t;
  1010. case PLUS_EXPR:
  1011. if (integer_zerop (arg0))
  1012. return convert (TREE_TYPE (expr), arg1);
  1013. if (integer_zerop (arg1))
  1014. return convert (TREE_TYPE (expr), arg0);
  1015. associate:
  1016. /* In most languages, can't associate operations on floats
  1017. through parentheses. Rather than remember where the parentheses
  1018. were, we don't associate floats at all. It shouldn't matter much. */
  1019. if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
  1020. goto binary;
  1021. /* The varsign == -1 cases happen only for addition and subtraction.
  1022. It says that the arg that was split was really CON minus VAR.
  1023. The rest of the code applies to all associative operations. */
  1024. if (!wins)
  1025. {
  1026. tree var, con, tem;
  1027. int varsign;
  1028. tree inner_arg;
  1029. if (split_tree (arg0, code, &var, &con, &varsign))
  1030. {
  1031. if (varsign == -1)
  1032. {
  1033. /* EXPR is (CON-VAR) +- ARG1. */
  1034. /* If it is + and VAR==ARG1, return just CONST. */
  1035. if (code == PLUS_EXPR && operand_equal_p (var, arg1))
  1036. return con;
  1037. /* Otherwise return (CON +- ARG1) - VAR. */
  1038. TREE_SET_CODE (t, MINUS_EXPR);
  1039. TREE_OPERAND (t, 1) = var;
  1040. TREE_OPERAND (t, 0)
  1041. = fold (build (code, TREE_TYPE (t), con, arg1));
  1042. }
  1043. else
  1044. {
  1045. /* EXPR is (VAR+CON) +- ARG1. */
  1046. /* If it is - and VAR==ARG1, return just CONST. */
  1047. if (code == MINUS_EXPR && operand_equal_p (var, arg1))
  1048. return con;
  1049. /* Otherwise return VAR +- (ARG1 +- CON). */
  1050. TREE_OPERAND (t, 1) = tem
  1051. = fold (build (code, TREE_TYPE (t), arg1, con));
  1052. TREE_OPERAND (t, 0) = var;
  1053. if (integer_zerop (tem)
  1054. && (code == PLUS_EXPR || code == MINUS_EXPR))
  1055. return var;
  1056. /* If we have x +/- (c - d) [c an explicit integer]
  1057. change it to x -/+ (d - c) since if d is relocatable
  1058. then the latter can be a single immediate insn
  1059. and the former cannot. */
  1060. if (TREE_CODE (tem) == MINUS_EXPR
  1061. && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
  1062. {
  1063. tree tem1 = TREE_OPERAND (tem, 1);
  1064. TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
  1065. TREE_OPERAND (tem, 0) = tem1;
  1066. TREE_SET_CODE (t,
  1067. (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  1068. }
  1069. }
  1070. return t;
  1071. }
  1072. if (split_tree (arg1, code, &var, &con, &varsign))
  1073. {
  1074. /* EXPR is ARG0 +- (CON +- VAR). */
  1075. if (varsign == -1)
  1076. TREE_SET_CODE (t,
  1077. (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  1078. if (TREE_CODE (t) == MINUS_EXPR && operand_equal_p (var, arg0))
  1079. return con;
  1080. TREE_OPERAND (t, 0)
  1081. = fold (build (code, TREE_TYPE (t), arg0, con));
  1082. TREE_OPERAND (t, 1) = var;
  1083. if (integer_zerop (TREE_OPERAND (t, 0))
  1084. && TREE_CODE (t) == PLUS_EXPR)
  1085. return var;
  1086. return t;
  1087. }
  1088. }
  1089. binary:
  1090. {
  1091. register tree t1 = NULL_TREE;
  1092. if (wins)
  1093. t1 = combine (code, arg0, arg1);
  1094. if (t1 != NULL_TREE) return t1;
  1095. return t;
  1096. }
  1097. case MINUS_EXPR:
  1098. if (! wins && integer_zerop (arg0))
  1099. return build (NEGATE_EXPR, TREE_TYPE (expr), arg1);
  1100. if (integer_zerop (arg1))
  1101. return convert (TREE_TYPE (expr), arg0);
  1102. /* Fold &x - &x. This can happen from &x.foo - &x. */
  1103. if (operand_equal_p (arg0, arg1))
  1104. return convert (TREE_TYPE (t), integer_zero_node);
  1105. goto associate;
  1106. case MULT_EXPR:
  1107. if (!loses && integer_zerop (arg0))
  1108. return convert (TREE_TYPE (expr), arg0);
  1109. if (!loses && integer_zerop (arg1))
  1110. return convert (TREE_TYPE (expr), arg1);
  1111. if (integer_onep (arg0))
  1112. return convert (TREE_TYPE (expr), arg1);
  1113. if (integer_onep (arg1))
  1114. return convert (TREE_TYPE (expr), arg0);
  1115. goto associate;
  1116. case BIT_IOR_EXPR:
  1117. if (!loses && integer_all_onesp (arg0))
  1118. return convert (TREE_TYPE (expr), arg0);
  1119. if (!loses && integer_all_onesp (arg1))
  1120. return convert (TREE_TYPE (expr), arg1);
  1121. case BIT_XOR_EXPR:
  1122. if (integer_zerop (arg0))
  1123. return convert (TREE_TYPE (expr), arg1);
  1124. if (integer_zerop (arg1))
  1125. return convert (TREE_TYPE (expr), arg0);
  1126. goto associate;
  1127. case BIT_AND_EXPR:
  1128. if (integer_all_onesp (arg0))
  1129. return convert (TREE_TYPE (expr), arg1);
  1130. if (integer_all_onesp (arg1))
  1131. return convert (TREE_TYPE (expr), arg0);
  1132. if (!loses && integer_zerop (arg0))
  1133. return convert (TREE_TYPE (expr), arg0);
  1134. if (!loses && integer_zerop (arg1))
  1135. return convert (TREE_TYPE (expr), arg1);
  1136. /* Simplify (int)((unsigned char)x & 0x377) into (int)(unsigned char)x. */
  1137. if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
  1138. && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  1139. {
  1140. int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
  1141. if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  1142. && (~TREE_INT_CST_LOW (arg0) & ((1 << prec) - 1)) == 0)
  1143. return convert (TREE_TYPE (expr), arg1);
  1144. }
  1145. if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
  1146. && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  1147. {
  1148. int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
  1149. if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  1150. && (~TREE_INT_CST_LOW (arg1) & ((1 << prec) - 1)) == 0)
  1151. return convert (TREE_TYPE (expr), arg0);
  1152. }
  1153. goto associate;
  1154. case BIT_ANDTC_EXPR:
  1155. if (integer_all_onesp (arg0))
  1156. return convert (TREE_TYPE (expr), arg1);
  1157. if (integer_zerop (arg1))
  1158. return convert (TREE_TYPE (expr), arg0);
  1159. if (!loses && integer_zerop (arg0))
  1160. return convert (TREE_TYPE (expr), arg0);
  1161. if (!loses && integer_all_onesp (arg1))
  1162. return combine (code, arg1, arg1);
  1163. goto binary;
  1164. case TRUNC_DIV_EXPR:
  1165. case ROUND_DIV_EXPR:
  1166. case FLOOR_DIV_EXPR:
  1167. case CEIL_DIV_EXPR:
  1168. case RDIV_EXPR:
  1169. if (integer_onep (arg1))
  1170. return convert (TREE_TYPE (expr), arg0);
  1171. goto binary;
  1172. case CEIL_MOD_EXPR:
  1173. case FLOOR_MOD_EXPR:
  1174. case ROUND_MOD_EXPR:
  1175. case TRUNC_MOD_EXPR:
  1176. if (!loses && integer_onep (arg1))
  1177. return combine (code, arg1, arg1);
  1178. goto binary;
  1179. case LSHIFT_EXPR:
  1180. case RSHIFT_EXPR:
  1181. case LROTATE_EXPR:
  1182. case RROTATE_EXPR:
  1183. if (integer_zerop (arg1))
  1184. return convert (TREE_TYPE (expr), arg0);
  1185. goto binary;
  1186. case MIN_EXPR: case MAX_EXPR:
  1187. goto associate;
  1188. case TRUTH_NOT_EXPR:
  1189. if (wins)
  1190. {
  1191. if (TREE_CODE (arg0) == INTEGER_CST)
  1192. {
  1193. t = build_int_2 ((TREE_INT_CST_LOW (arg0) == 0
  1194. && TREE_INT_CST_HIGH (arg0) == 0),
  1195. 0);
  1196. TREE_TYPE (t) = integer_type_node;
  1197. }
  1198. if (TREE_CODE (arg0) == REAL_CST)
  1199. {
  1200. t = build_int_2 (TREE_REAL_CST (arg0) == 0, 0);
  1201. TREE_TYPE (t) = integer_type_node;
  1202. }
  1203. }
  1204. return t;
  1205. case TRUTH_AND_EXPR:
  1206. case TRUTH_ANDIF_EXPR:
  1207. if (wins)
  1208. {
  1209. if (TREE_CODE (arg0) == INTEGER_CST
  1210. && TREE_CODE (arg1) == INTEGER_CST)
  1211. t = build_int_2 (((TREE_INT_CST_LOW (arg0) || TREE_INT_CST_HIGH (arg0))
  1212. && (TREE_INT_CST_LOW (arg1) || TREE_INT_CST_HIGH (arg1))),
  1213. 0);
  1214. if (TREE_CODE (arg0) == REAL_CST
  1215. && TREE_CODE (arg1) == REAL_CST)
  1216. t = build_int_2 ((TREE_REAL_CST (arg0) && TREE_REAL_CST (arg1)),
  1217. 0);
  1218. TREE_TYPE (t) = TREE_TYPE (expr);
  1219. }
  1220. return t;
  1221. case TRUTH_OR_EXPR:
  1222. case TRUTH_ORIF_EXPR:
  1223. if (wins)
  1224. {
  1225. if (TREE_CODE (arg0) == INTEGER_CST
  1226. && TREE_CODE (arg1) == INTEGER_CST)
  1227. t = build_int_2 (((TREE_INT_CST_LOW (arg0) || TREE_INT_CST_HIGH (arg0))
  1228. || (TREE_INT_CST_LOW (arg1) || TREE_INT_CST_HIGH (arg1))),
  1229. 0);
  1230. if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
  1231. t = build_int_2 ((TREE_REAL_CST (arg0) || TREE_REAL_CST (arg1)),
  1232. 0);
  1233. TREE_TYPE (t) = TREE_TYPE (expr);
  1234. }
  1235. return t;
  1236. case EQ_EXPR:
  1237. case NE_EXPR:
  1238. case LT_EXPR:
  1239. case GT_EXPR:
  1240. case LE_EXPR:
  1241. case GE_EXPR:
  1242. /* Convert foo++ == CONST into ++foo == CONST + INCR.
  1243. First, see if one arg is constant; find the constant arg
  1244. and the other one. */
  1245. {
  1246. tree constop = 0, varop;
  1247. tree *constoploc;
  1248. if (TREE_LITERAL (arg1))
  1249. constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
  1250. if (TREE_LITERAL (arg0))
  1251. constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
  1252. if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
  1253. {
  1254. tree newconst
  1255. = fold (build (PLUS_EXPR, TREE_TYPE (t),
  1256. constop, TREE_OPERAND (varop, 1)));
  1257. /* This optimization is invalid for ordered comparisons
  1258. if CONST+INCR overflows!
  1259. We can assume that address + integer will not overflow. */
  1260. if (TREE_CODE (newconst) != INTEGER_CST
  1261. || ! tree_int_cst_lt (newconst, constop)
  1262. || code == EQ_EXPR || code == NE_EXPR)
  1263. {
  1264. TREE_CODE (varop) = PREINCREMENT_EXPR;
  1265. *constoploc = newconst;
  1266. return t;
  1267. }
  1268. }
  1269. else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
  1270. {
  1271. tree newconst
  1272. = fold (build (MINUS_EXPR, TREE_TYPE (t),
  1273. constop, TREE_OPERAND (varop, 1)));
  1274. if (TREE_CODE (newconst) != INTEGER_CST
  1275. || tree_int_cst_lt (newconst, constop)
  1276. || code == EQ_EXPR || code == NE_EXPR)
  1277. {
  1278. TREE_CODE (varop) = PREDECREMENT_EXPR;
  1279. *constoploc = newconst;
  1280. return t;
  1281. }
  1282. }
  1283. }
  1284. /* To compute GT, swap the arguments and do LT.
  1285. To compute GE, do LT and invert the result.
  1286. To compute LE, swap the arguments, do LT and invert the result.
  1287. To compute NE, do EQ and invert the result. */
  1288. if (code == LE_EXPR || code == GT_EXPR)
  1289. {
  1290. register tree temp = arg0;
  1291. arg0 = arg1;
  1292. arg1 = temp;
  1293. }
  1294. /* Compute a result for LT or EQ if args permit. */
  1295. if (TREE_CODE (arg0) == INTEGER_CST
  1296. && TREE_CODE (arg1) == INTEGER_CST)
  1297. {
  1298. if (code == EQ_EXPR || code == NE_EXPR)
  1299. t = build_int_2
  1300. (TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  1301. && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1),
  1302. 0);
  1303. else
  1304. t = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
  1305. ? INT_CST_LT_UNSIGNED (arg0, arg1)
  1306. : INT_CST_LT (arg0, arg1)),
  1307. 0);
  1308. }
  1309. else if (TREE_CODE (arg0) == REAL_CST
  1310. && TREE_CODE (arg1) == REAL_CST)
  1311. {
  1312. if (code == EQ_EXPR || code == NE_EXPR)
  1313. t = build_int_2 (TREE_REAL_CST (arg0) == TREE_REAL_CST (arg1), 0);
  1314. else
  1315. t = build_int_2 (TREE_REAL_CST (arg0) < TREE_REAL_CST (arg1), 0);
  1316. }
  1317. else
  1318. return t;
  1319. /* If we wanted ...-or-equal, invert the result. */
  1320. if (code == GE_EXPR || code == LE_EXPR || code == NE_EXPR)
  1321. TREE_INT_CST_LOW (t) ^= 1;
  1322. TREE_TYPE (t) = TREE_TYPE (expr);
  1323. return t;
  1324. COND_EXPR:
  1325. if (TREE_LITERAL (arg0))
  1326. return TREE_OPERAND (expr, (integer_zerop (arg0) ? 2 : 1));
  1327. return t;
  1328. default:
  1329. return t;
  1330. } /* switch (code) */
  1331. }