convert.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /* Utility routines for data type conversion for GCC.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* These routines are somewhat language-independent utility function
  16. intended to be called by the language-specific convert () functions. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "real.h"
  31. #include "fixed-value.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stor-layout.h"
  35. #include "flags.h"
  36. #include "convert.h"
  37. #include "diagnostic-core.h"
  38. #include "target.h"
  39. #include "langhooks.h"
  40. #include "builtins.h"
  41. #include "ubsan.h"
  42. /* Convert EXPR to some pointer or reference type TYPE.
  43. EXPR must be pointer, reference, integer, enumeral, or literal zero;
  44. in other cases error is called. */
  45. tree
  46. convert_to_pointer (tree type, tree expr)
  47. {
  48. location_t loc = EXPR_LOCATION (expr);
  49. if (TREE_TYPE (expr) == type)
  50. return expr;
  51. switch (TREE_CODE (TREE_TYPE (expr)))
  52. {
  53. case POINTER_TYPE:
  54. case REFERENCE_TYPE:
  55. {
  56. /* If the pointers point to different address spaces, conversion needs
  57. to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
  58. addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
  59. addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
  60. if (to_as == from_as)
  61. return fold_build1_loc (loc, NOP_EXPR, type, expr);
  62. else
  63. return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
  64. }
  65. case INTEGER_TYPE:
  66. case ENUMERAL_TYPE:
  67. case BOOLEAN_TYPE:
  68. {
  69. /* If the input precision differs from the target pointer type
  70. precision, first convert the input expression to an integer type of
  71. the target precision. Some targets, e.g. VMS, need several pointer
  72. sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
  73. unsigned int pprec = TYPE_PRECISION (type);
  74. unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
  75. if (eprec != pprec)
  76. expr = fold_build1_loc (loc, NOP_EXPR,
  77. lang_hooks.types.type_for_size (pprec, 0),
  78. expr);
  79. }
  80. return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
  81. default:
  82. error ("cannot convert to a pointer type");
  83. return convert_to_pointer (type, integer_zero_node);
  84. }
  85. }
  86. /* Convert EXPR to some floating-point type TYPE.
  87. EXPR must be float, fixed-point, integer, or enumeral;
  88. in other cases error is called. */
  89. tree
  90. convert_to_real (tree type, tree expr)
  91. {
  92. enum built_in_function fcode = builtin_mathfn_code (expr);
  93. tree itype = TREE_TYPE (expr);
  94. if (TREE_CODE (expr) == COMPOUND_EXPR)
  95. {
  96. tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
  97. if (t == TREE_OPERAND (expr, 1))
  98. return expr;
  99. return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
  100. TREE_OPERAND (expr, 0), t);
  101. }
  102. /* Disable until we figure out how to decide whether the functions are
  103. present in runtime. */
  104. /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
  105. if (optimize
  106. && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
  107. || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
  108. {
  109. switch (fcode)
  110. {
  111. #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
  112. CASE_MATHFN (COSH)
  113. CASE_MATHFN (EXP)
  114. CASE_MATHFN (EXP10)
  115. CASE_MATHFN (EXP2)
  116. CASE_MATHFN (EXPM1)
  117. CASE_MATHFN (GAMMA)
  118. CASE_MATHFN (J0)
  119. CASE_MATHFN (J1)
  120. CASE_MATHFN (LGAMMA)
  121. CASE_MATHFN (POW10)
  122. CASE_MATHFN (SINH)
  123. CASE_MATHFN (TGAMMA)
  124. CASE_MATHFN (Y0)
  125. CASE_MATHFN (Y1)
  126. /* The above functions may set errno differently with float
  127. input or output so this transformation is not safe with
  128. -fmath-errno. */
  129. if (flag_errno_math)
  130. break;
  131. CASE_MATHFN (ACOS)
  132. CASE_MATHFN (ACOSH)
  133. CASE_MATHFN (ASIN)
  134. CASE_MATHFN (ASINH)
  135. CASE_MATHFN (ATAN)
  136. CASE_MATHFN (ATANH)
  137. CASE_MATHFN (CBRT)
  138. CASE_MATHFN (COS)
  139. CASE_MATHFN (ERF)
  140. CASE_MATHFN (ERFC)
  141. CASE_MATHFN (LOG)
  142. CASE_MATHFN (LOG10)
  143. CASE_MATHFN (LOG2)
  144. CASE_MATHFN (LOG1P)
  145. CASE_MATHFN (SIN)
  146. CASE_MATHFN (TAN)
  147. CASE_MATHFN (TANH)
  148. /* The above functions are not safe to do this conversion. */
  149. if (!flag_unsafe_math_optimizations)
  150. break;
  151. CASE_MATHFN (SQRT)
  152. CASE_MATHFN (FABS)
  153. CASE_MATHFN (LOGB)
  154. #undef CASE_MATHFN
  155. {
  156. tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
  157. tree newtype = type;
  158. /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
  159. the both as the safe type for operation. */
  160. if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
  161. newtype = TREE_TYPE (arg0);
  162. /* We consider to convert
  163. (T1) sqrtT2 ((T2) exprT3)
  164. to
  165. (T1) sqrtT4 ((T4) exprT3)
  166. , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
  167. and T4 is NEWTYPE. All those types are of floating point types.
  168. T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
  169. is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
  170. T2 and T4. See the following URL for a reference:
  171. http://stackoverflow.com/questions/9235456/determining-
  172. floating-point-square-root
  173. */
  174. if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
  175. && !flag_unsafe_math_optimizations)
  176. {
  177. /* The following conversion is unsafe even the precision condition
  178. below is satisfied:
  179. (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
  180. */
  181. if (TYPE_MODE (type) != TYPE_MODE (newtype))
  182. break;
  183. int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
  184. int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
  185. if (p1 < p2 * 2 + 2)
  186. break;
  187. }
  188. /* Be careful about integer to fp conversions.
  189. These may overflow still. */
  190. if (FLOAT_TYPE_P (TREE_TYPE (arg0))
  191. && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
  192. && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
  193. || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
  194. {
  195. tree fn = mathfn_built_in (newtype, fcode);
  196. if (fn)
  197. {
  198. tree arg = fold (convert_to_real (newtype, arg0));
  199. expr = build_call_expr (fn, 1, arg);
  200. if (newtype == type)
  201. return expr;
  202. }
  203. }
  204. }
  205. default:
  206. break;
  207. }
  208. }
  209. if (optimize
  210. && (((fcode == BUILT_IN_FLOORL
  211. || fcode == BUILT_IN_CEILL
  212. || fcode == BUILT_IN_ROUNDL
  213. || fcode == BUILT_IN_RINTL
  214. || fcode == BUILT_IN_TRUNCL
  215. || fcode == BUILT_IN_NEARBYINTL)
  216. && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
  217. || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
  218. || ((fcode == BUILT_IN_FLOOR
  219. || fcode == BUILT_IN_CEIL
  220. || fcode == BUILT_IN_ROUND
  221. || fcode == BUILT_IN_RINT
  222. || fcode == BUILT_IN_TRUNC
  223. || fcode == BUILT_IN_NEARBYINT)
  224. && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
  225. {
  226. tree fn = mathfn_built_in (type, fcode);
  227. if (fn)
  228. {
  229. tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
  230. /* Make sure (type)arg0 is an extension, otherwise we could end up
  231. changing (float)floor(double d) into floorf((float)d), which is
  232. incorrect because (float)d uses round-to-nearest and can round
  233. up to the next integer. */
  234. if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
  235. return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
  236. }
  237. }
  238. /* Propagate the cast into the operation. */
  239. if (itype != type && FLOAT_TYPE_P (type))
  240. switch (TREE_CODE (expr))
  241. {
  242. /* Convert (float)-x into -(float)x. This is safe for
  243. round-to-nearest rounding mode when the inner type is float. */
  244. case ABS_EXPR:
  245. case NEGATE_EXPR:
  246. if (!flag_rounding_math
  247. && FLOAT_TYPE_P (itype)
  248. && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
  249. return build1 (TREE_CODE (expr), type,
  250. fold (convert_to_real (type,
  251. TREE_OPERAND (expr, 0))));
  252. break;
  253. /* Convert (outertype)((innertype0)a+(innertype1)b)
  254. into ((newtype)a+(newtype)b) where newtype
  255. is the widest mode from all of these. */
  256. case PLUS_EXPR:
  257. case MINUS_EXPR:
  258. case MULT_EXPR:
  259. case RDIV_EXPR:
  260. {
  261. tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
  262. tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
  263. if (FLOAT_TYPE_P (TREE_TYPE (arg0))
  264. && FLOAT_TYPE_P (TREE_TYPE (arg1))
  265. && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
  266. {
  267. tree newtype = type;
  268. if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
  269. || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
  270. || TYPE_MODE (type) == SDmode)
  271. newtype = dfloat32_type_node;
  272. if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
  273. || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
  274. || TYPE_MODE (type) == DDmode)
  275. newtype = dfloat64_type_node;
  276. if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
  277. || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
  278. || TYPE_MODE (type) == TDmode)
  279. newtype = dfloat128_type_node;
  280. if (newtype == dfloat32_type_node
  281. || newtype == dfloat64_type_node
  282. || newtype == dfloat128_type_node)
  283. {
  284. expr = build2 (TREE_CODE (expr), newtype,
  285. fold (convert_to_real (newtype, arg0)),
  286. fold (convert_to_real (newtype, arg1)));
  287. if (newtype == type)
  288. return expr;
  289. break;
  290. }
  291. if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
  292. newtype = TREE_TYPE (arg0);
  293. if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
  294. newtype = TREE_TYPE (arg1);
  295. /* Sometimes this transformation is safe (cannot
  296. change results through affecting double rounding
  297. cases) and sometimes it is not. If NEWTYPE is
  298. wider than TYPE, e.g. (float)((long double)double
  299. + (long double)double) converted to
  300. (float)(double + double), the transformation is
  301. unsafe regardless of the details of the types
  302. involved; double rounding can arise if the result
  303. of NEWTYPE arithmetic is a NEWTYPE value half way
  304. between two representable TYPE values but the
  305. exact value is sufficiently different (in the
  306. right direction) for this difference to be
  307. visible in ITYPE arithmetic. If NEWTYPE is the
  308. same as TYPE, however, the transformation may be
  309. safe depending on the types involved: it is safe
  310. if the ITYPE has strictly more than twice as many
  311. mantissa bits as TYPE, can represent infinities
  312. and NaNs if the TYPE can, and has sufficient
  313. exponent range for the product or ratio of two
  314. values representable in the TYPE to be within the
  315. range of normal values of ITYPE. */
  316. if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
  317. && (flag_unsafe_math_optimizations
  318. || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
  319. && real_can_shorten_arithmetic (TYPE_MODE (itype),
  320. TYPE_MODE (type))
  321. && !excess_precision_type (newtype))))
  322. {
  323. expr = build2 (TREE_CODE (expr), newtype,
  324. fold (convert_to_real (newtype, arg0)),
  325. fold (convert_to_real (newtype, arg1)));
  326. if (newtype == type)
  327. return expr;
  328. }
  329. }
  330. }
  331. break;
  332. default:
  333. break;
  334. }
  335. switch (TREE_CODE (TREE_TYPE (expr)))
  336. {
  337. case REAL_TYPE:
  338. /* Ignore the conversion if we don't need to store intermediate
  339. results and neither type is a decimal float. */
  340. return build1 ((flag_float_store
  341. || DECIMAL_FLOAT_TYPE_P (type)
  342. || DECIMAL_FLOAT_TYPE_P (itype))
  343. ? CONVERT_EXPR : NOP_EXPR, type, expr);
  344. case INTEGER_TYPE:
  345. case ENUMERAL_TYPE:
  346. case BOOLEAN_TYPE:
  347. return build1 (FLOAT_EXPR, type, expr);
  348. case FIXED_POINT_TYPE:
  349. return build1 (FIXED_CONVERT_EXPR, type, expr);
  350. case COMPLEX_TYPE:
  351. return convert (type,
  352. fold_build1 (REALPART_EXPR,
  353. TREE_TYPE (TREE_TYPE (expr)), expr));
  354. case POINTER_TYPE:
  355. case REFERENCE_TYPE:
  356. error ("pointer value used where a floating point value was expected");
  357. return convert_to_real (type, integer_zero_node);
  358. default:
  359. error ("aggregate value used where a float was expected");
  360. return convert_to_real (type, integer_zero_node);
  361. }
  362. }
  363. /* Convert EXPR to some integer (or enum) type TYPE.
  364. EXPR must be pointer, integer, discrete (enum, char, or bool), float,
  365. fixed-point or vector; in other cases error is called.
  366. The result of this is always supposed to be a newly created tree node
  367. not in use in any existing structure. */
  368. tree
  369. convert_to_integer (tree type, tree expr)
  370. {
  371. enum tree_code ex_form = TREE_CODE (expr);
  372. tree intype = TREE_TYPE (expr);
  373. unsigned int inprec = element_precision (intype);
  374. unsigned int outprec = element_precision (type);
  375. location_t loc = EXPR_LOCATION (expr);
  376. /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
  377. be. Consider `enum E = { a, b = (enum E) 3 };'. */
  378. if (!COMPLETE_TYPE_P (type))
  379. {
  380. error ("conversion to incomplete type");
  381. return error_mark_node;
  382. }
  383. if (ex_form == COMPOUND_EXPR)
  384. {
  385. tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
  386. if (t == TREE_OPERAND (expr, 1))
  387. return expr;
  388. return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
  389. TREE_OPERAND (expr, 0), t);
  390. }
  391. /* Convert e.g. (long)round(d) -> lround(d). */
  392. /* If we're converting to char, we may encounter differing behavior
  393. between converting from double->char vs double->long->char.
  394. We're in "undefined" territory but we prefer to be conservative,
  395. so only proceed in "unsafe" math mode. */
  396. if (optimize
  397. && (flag_unsafe_math_optimizations
  398. || (long_integer_type_node
  399. && outprec >= TYPE_PRECISION (long_integer_type_node))))
  400. {
  401. tree s_expr = strip_float_extensions (expr);
  402. tree s_intype = TREE_TYPE (s_expr);
  403. const enum built_in_function fcode = builtin_mathfn_code (s_expr);
  404. tree fn = 0;
  405. switch (fcode)
  406. {
  407. CASE_FLT_FN (BUILT_IN_CEIL):
  408. /* Only convert in ISO C99 mode. */
  409. if (!targetm.libc_has_function (function_c99_misc))
  410. break;
  411. if (outprec < TYPE_PRECISION (integer_type_node)
  412. || (outprec == TYPE_PRECISION (integer_type_node)
  413. && !TYPE_UNSIGNED (type)))
  414. fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
  415. else if (outprec == TYPE_PRECISION (long_integer_type_node)
  416. && !TYPE_UNSIGNED (type))
  417. fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
  418. else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
  419. && !TYPE_UNSIGNED (type))
  420. fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
  421. break;
  422. CASE_FLT_FN (BUILT_IN_FLOOR):
  423. /* Only convert in ISO C99 mode. */
  424. if (!targetm.libc_has_function (function_c99_misc))
  425. break;
  426. if (outprec < TYPE_PRECISION (integer_type_node)
  427. || (outprec == TYPE_PRECISION (integer_type_node)
  428. && !TYPE_UNSIGNED (type)))
  429. fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
  430. else if (outprec == TYPE_PRECISION (long_integer_type_node)
  431. && !TYPE_UNSIGNED (type))
  432. fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
  433. else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
  434. && !TYPE_UNSIGNED (type))
  435. fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
  436. break;
  437. CASE_FLT_FN (BUILT_IN_ROUND):
  438. /* Only convert in ISO C99 mode and with -fno-math-errno. */
  439. if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
  440. break;
  441. if (outprec < TYPE_PRECISION (integer_type_node)
  442. || (outprec == TYPE_PRECISION (integer_type_node)
  443. && !TYPE_UNSIGNED (type)))
  444. fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
  445. else if (outprec == TYPE_PRECISION (long_integer_type_node)
  446. && !TYPE_UNSIGNED (type))
  447. fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
  448. else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
  449. && !TYPE_UNSIGNED (type))
  450. fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
  451. break;
  452. CASE_FLT_FN (BUILT_IN_NEARBYINT):
  453. /* Only convert nearbyint* if we can ignore math exceptions. */
  454. if (flag_trapping_math)
  455. break;
  456. /* ... Fall through ... */
  457. CASE_FLT_FN (BUILT_IN_RINT):
  458. /* Only convert in ISO C99 mode and with -fno-math-errno. */
  459. if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
  460. break;
  461. if (outprec < TYPE_PRECISION (integer_type_node)
  462. || (outprec == TYPE_PRECISION (integer_type_node)
  463. && !TYPE_UNSIGNED (type)))
  464. fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
  465. else if (outprec == TYPE_PRECISION (long_integer_type_node)
  466. && !TYPE_UNSIGNED (type))
  467. fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
  468. else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
  469. && !TYPE_UNSIGNED (type))
  470. fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
  471. break;
  472. CASE_FLT_FN (BUILT_IN_TRUNC):
  473. return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
  474. default:
  475. break;
  476. }
  477. if (fn)
  478. {
  479. tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
  480. return convert_to_integer (type, newexpr);
  481. }
  482. }
  483. /* Convert (int)logb(d) -> ilogb(d). */
  484. if (optimize
  485. && flag_unsafe_math_optimizations
  486. && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
  487. && integer_type_node
  488. && (outprec > TYPE_PRECISION (integer_type_node)
  489. || (outprec == TYPE_PRECISION (integer_type_node)
  490. && !TYPE_UNSIGNED (type))))
  491. {
  492. tree s_expr = strip_float_extensions (expr);
  493. tree s_intype = TREE_TYPE (s_expr);
  494. const enum built_in_function fcode = builtin_mathfn_code (s_expr);
  495. tree fn = 0;
  496. switch (fcode)
  497. {
  498. CASE_FLT_FN (BUILT_IN_LOGB):
  499. fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
  500. break;
  501. default:
  502. break;
  503. }
  504. if (fn)
  505. {
  506. tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
  507. return convert_to_integer (type, newexpr);
  508. }
  509. }
  510. switch (TREE_CODE (intype))
  511. {
  512. case POINTER_TYPE:
  513. case REFERENCE_TYPE:
  514. if (integer_zerop (expr))
  515. return build_int_cst (type, 0);
  516. /* Convert to an unsigned integer of the correct width first, and from
  517. there widen/truncate to the required type. Some targets support the
  518. coexistence of multiple valid pointer sizes, so fetch the one we need
  519. from the type. */
  520. expr = fold_build1 (CONVERT_EXPR,
  521. lang_hooks.types.type_for_size
  522. (TYPE_PRECISION (intype), 0),
  523. expr);
  524. return fold_convert (type, expr);
  525. case INTEGER_TYPE:
  526. case ENUMERAL_TYPE:
  527. case BOOLEAN_TYPE:
  528. case OFFSET_TYPE:
  529. /* If this is a logical operation, which just returns 0 or 1, we can
  530. change the type of the expression. */
  531. if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
  532. {
  533. expr = copy_node (expr);
  534. TREE_TYPE (expr) = type;
  535. return expr;
  536. }
  537. /* If we are widening the type, put in an explicit conversion.
  538. Similarly if we are not changing the width. After this, we know
  539. we are truncating EXPR. */
  540. else if (outprec >= inprec)
  541. {
  542. enum tree_code code;
  543. /* If the precision of the EXPR's type is K bits and the
  544. destination mode has more bits, and the sign is changing,
  545. it is not safe to use a NOP_EXPR. For example, suppose
  546. that EXPR's type is a 3-bit unsigned integer type, the
  547. TYPE is a 3-bit signed integer type, and the machine mode
  548. for the types is 8-bit QImode. In that case, the
  549. conversion necessitates an explicit sign-extension. In
  550. the signed-to-unsigned case the high-order bits have to
  551. be cleared. */
  552. if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
  553. && (TYPE_PRECISION (TREE_TYPE (expr))
  554. != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
  555. code = CONVERT_EXPR;
  556. else
  557. code = NOP_EXPR;
  558. return fold_build1 (code, type, expr);
  559. }
  560. /* If TYPE is an enumeral type or a type with a precision less
  561. than the number of bits in its mode, do the conversion to the
  562. type corresponding to its mode, then do a nop conversion
  563. to TYPE. */
  564. else if (TREE_CODE (type) == ENUMERAL_TYPE
  565. || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
  566. return build1 (NOP_EXPR, type,
  567. convert (lang_hooks.types.type_for_mode
  568. (TYPE_MODE (type), TYPE_UNSIGNED (type)),
  569. expr));
  570. /* Here detect when we can distribute the truncation down past some
  571. arithmetic. For example, if adding two longs and converting to an
  572. int, we can equally well convert both to ints and then add.
  573. For the operations handled here, such truncation distribution
  574. is always safe.
  575. It is desirable in these cases:
  576. 1) when truncating down to full-word from a larger size
  577. 2) when truncating takes no work.
  578. 3) when at least one operand of the arithmetic has been extended
  579. (as by C's default conversions). In this case we need two conversions
  580. if we do the arithmetic as already requested, so we might as well
  581. truncate both and then combine. Perhaps that way we need only one.
  582. Note that in general we cannot do the arithmetic in a type
  583. shorter than the desired result of conversion, even if the operands
  584. are both extended from a shorter type, because they might overflow
  585. if combined in that type. The exceptions to this--the times when
  586. two narrow values can be combined in their narrow type even to
  587. make a wider result--are handled by "shorten" in build_binary_op. */
  588. switch (ex_form)
  589. {
  590. case RSHIFT_EXPR:
  591. /* We can pass truncation down through right shifting
  592. when the shift count is a nonpositive constant. */
  593. if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  594. && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
  595. goto trunc1;
  596. break;
  597. case LSHIFT_EXPR:
  598. /* We can pass truncation down through left shifting
  599. when the shift count is a nonnegative constant and
  600. the target type is unsigned. */
  601. if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
  602. && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
  603. && TYPE_UNSIGNED (type)
  604. && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  605. {
  606. /* If shift count is less than the width of the truncated type,
  607. really shift. */
  608. if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
  609. /* In this case, shifting is like multiplication. */
  610. goto trunc1;
  611. else
  612. {
  613. /* If it is >= that width, result is zero.
  614. Handling this with trunc1 would give the wrong result:
  615. (int) ((long long) a << 32) is well defined (as 0)
  616. but (int) a << 32 is undefined and would get a
  617. warning. */
  618. tree t = build_int_cst (type, 0);
  619. /* If the original expression had side-effects, we must
  620. preserve it. */
  621. if (TREE_SIDE_EFFECTS (expr))
  622. return build2 (COMPOUND_EXPR, type, expr, t);
  623. else
  624. return t;
  625. }
  626. }
  627. break;
  628. case TRUNC_DIV_EXPR:
  629. {
  630. tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  631. tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  632. /* Don't distribute unless the output precision is at least as big
  633. as the actual inputs and it has the same signedness. */
  634. if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  635. && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  636. /* If signedness of arg0 and arg1 don't match,
  637. we can't necessarily find a type to compare them in. */
  638. && (TYPE_UNSIGNED (TREE_TYPE (arg0))
  639. == TYPE_UNSIGNED (TREE_TYPE (arg1)))
  640. /* Do not change the sign of the division. */
  641. && (TYPE_UNSIGNED (TREE_TYPE (expr))
  642. == TYPE_UNSIGNED (TREE_TYPE (arg0)))
  643. /* Either require unsigned division or a division by
  644. a constant that is not -1. */
  645. && (TYPE_UNSIGNED (TREE_TYPE (arg0))
  646. || (TREE_CODE (arg1) == INTEGER_CST
  647. && !integer_all_onesp (arg1))))
  648. goto trunc1;
  649. break;
  650. }
  651. case MAX_EXPR:
  652. case MIN_EXPR:
  653. case MULT_EXPR:
  654. {
  655. tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  656. tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  657. /* Don't distribute unless the output precision is at least as big
  658. as the actual inputs. Otherwise, the comparison of the
  659. truncated values will be wrong. */
  660. if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  661. && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  662. /* If signedness of arg0 and arg1 don't match,
  663. we can't necessarily find a type to compare them in. */
  664. && (TYPE_UNSIGNED (TREE_TYPE (arg0))
  665. == TYPE_UNSIGNED (TREE_TYPE (arg1))))
  666. goto trunc1;
  667. break;
  668. }
  669. case PLUS_EXPR:
  670. case MINUS_EXPR:
  671. case BIT_AND_EXPR:
  672. case BIT_IOR_EXPR:
  673. case BIT_XOR_EXPR:
  674. trunc1:
  675. {
  676. tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  677. tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  678. /* Do not try to narrow operands of pointer subtraction;
  679. that will interfere with other folding. */
  680. if (ex_form == MINUS_EXPR
  681. && CONVERT_EXPR_P (arg0)
  682. && CONVERT_EXPR_P (arg1)
  683. && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
  684. && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  685. break;
  686. if (outprec >= BITS_PER_WORD
  687. || TRULY_NOOP_TRUNCATION (outprec, inprec)
  688. || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  689. || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  690. {
  691. /* Do the arithmetic in type TYPEX,
  692. then convert result to TYPE. */
  693. tree typex = type;
  694. /* Can't do arithmetic in enumeral types
  695. so use an integer type that will hold the values. */
  696. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  697. typex
  698. = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
  699. TYPE_UNSIGNED (typex));
  700. /* But now perhaps TYPEX is as wide as INPREC.
  701. In that case, do nothing special here.
  702. (Otherwise would recurse infinitely in convert. */
  703. if (TYPE_PRECISION (typex) != inprec)
  704. {
  705. /* Don't do unsigned arithmetic where signed was wanted,
  706. or vice versa.
  707. Exception: if both of the original operands were
  708. unsigned then we can safely do the work as unsigned.
  709. Exception: shift operations take their type solely
  710. from the first argument.
  711. Exception: the LSHIFT_EXPR case above requires that
  712. we perform this operation unsigned lest we produce
  713. signed-overflow undefinedness.
  714. And we may need to do it as unsigned
  715. if we truncate to the original size. */
  716. if (TYPE_UNSIGNED (TREE_TYPE (expr))
  717. || (TYPE_UNSIGNED (TREE_TYPE (arg0))
  718. && (TYPE_UNSIGNED (TREE_TYPE (arg1))
  719. || ex_form == LSHIFT_EXPR
  720. || ex_form == RSHIFT_EXPR
  721. || ex_form == LROTATE_EXPR
  722. || ex_form == RROTATE_EXPR))
  723. || ex_form == LSHIFT_EXPR
  724. /* If we have !flag_wrapv, and either ARG0 or
  725. ARG1 is of a signed type, we have to do
  726. PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
  727. type in case the operation in outprec precision
  728. could overflow. Otherwise, we would introduce
  729. signed-overflow undefinedness. */
  730. || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
  731. || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
  732. && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
  733. > outprec)
  734. || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
  735. > outprec))
  736. && (ex_form == PLUS_EXPR
  737. || ex_form == MINUS_EXPR
  738. || ex_form == MULT_EXPR)))
  739. {
  740. if (!TYPE_UNSIGNED (typex))
  741. typex = unsigned_type_for (typex);
  742. }
  743. else
  744. {
  745. if (TYPE_UNSIGNED (typex))
  746. typex = signed_type_for (typex);
  747. }
  748. return convert (type,
  749. fold_build2 (ex_form, typex,
  750. convert (typex, arg0),
  751. convert (typex, arg1)));
  752. }
  753. }
  754. }
  755. break;
  756. case NEGATE_EXPR:
  757. case BIT_NOT_EXPR:
  758. /* This is not correct for ABS_EXPR,
  759. since we must test the sign before truncation. */
  760. {
  761. /* Do the arithmetic in type TYPEX,
  762. then convert result to TYPE. */
  763. tree typex = type;
  764. /* Can't do arithmetic in enumeral types
  765. so use an integer type that will hold the values. */
  766. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  767. typex
  768. = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
  769. TYPE_UNSIGNED (typex));
  770. if (!TYPE_UNSIGNED (typex))
  771. typex = unsigned_type_for (typex);
  772. return convert (type,
  773. fold_build1 (ex_form, typex,
  774. convert (typex,
  775. TREE_OPERAND (expr, 0))));
  776. }
  777. CASE_CONVERT:
  778. /* Don't introduce a
  779. "can't convert between vector values of different size" error. */
  780. if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
  781. && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
  782. != GET_MODE_SIZE (TYPE_MODE (type))))
  783. break;
  784. /* If truncating after truncating, might as well do all at once.
  785. If truncating after extending, we may get rid of wasted work. */
  786. return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  787. case COND_EXPR:
  788. /* It is sometimes worthwhile to push the narrowing down through
  789. the conditional and never loses. A COND_EXPR may have a throw
  790. as one operand, which then has void type. Just leave void
  791. operands as they are. */
  792. return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
  793. VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
  794. ? TREE_OPERAND (expr, 1)
  795. : convert (type, TREE_OPERAND (expr, 1)),
  796. VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
  797. ? TREE_OPERAND (expr, 2)
  798. : convert (type, TREE_OPERAND (expr, 2)));
  799. default:
  800. break;
  801. }
  802. /* When parsing long initializers, we might end up with a lot of casts.
  803. Shortcut this. */
  804. if (TREE_CODE (expr) == INTEGER_CST)
  805. return fold_convert (type, expr);
  806. return build1 (CONVERT_EXPR, type, expr);
  807. case REAL_TYPE:
  808. if (flag_sanitize & SANITIZE_FLOAT_CAST
  809. && do_ubsan_in_current_function ())
  810. {
  811. expr = save_expr (expr);
  812. tree check = ubsan_instrument_float_cast (loc, type, expr, expr);
  813. expr = build1 (FIX_TRUNC_EXPR, type, expr);
  814. if (check == NULL)
  815. return expr;
  816. return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
  817. }
  818. else
  819. return build1 (FIX_TRUNC_EXPR, type, expr);
  820. case FIXED_POINT_TYPE:
  821. return build1 (FIXED_CONVERT_EXPR, type, expr);
  822. case COMPLEX_TYPE:
  823. return convert (type,
  824. fold_build1 (REALPART_EXPR,
  825. TREE_TYPE (TREE_TYPE (expr)), expr));
  826. case VECTOR_TYPE:
  827. if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
  828. {
  829. error ("can%'t convert between vector values of different size");
  830. return error_mark_node;
  831. }
  832. return build1 (VIEW_CONVERT_EXPR, type, expr);
  833. default:
  834. error ("aggregate value used where an integer was expected");
  835. return convert (type, integer_zero_node);
  836. }
  837. }
  838. /* Convert EXPR to the complex type TYPE in the usual ways. */
  839. tree
  840. convert_to_complex (tree type, tree expr)
  841. {
  842. tree subtype = TREE_TYPE (type);
  843. switch (TREE_CODE (TREE_TYPE (expr)))
  844. {
  845. case REAL_TYPE:
  846. case FIXED_POINT_TYPE:
  847. case INTEGER_TYPE:
  848. case ENUMERAL_TYPE:
  849. case BOOLEAN_TYPE:
  850. return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
  851. convert (subtype, integer_zero_node));
  852. case COMPLEX_TYPE:
  853. {
  854. tree elt_type = TREE_TYPE (TREE_TYPE (expr));
  855. if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
  856. return expr;
  857. else if (TREE_CODE (expr) == COMPOUND_EXPR)
  858. {
  859. tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
  860. if (t == TREE_OPERAND (expr, 1))
  861. return expr;
  862. return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
  863. TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
  864. }
  865. else if (TREE_CODE (expr) == COMPLEX_EXPR)
  866. return fold_build2 (COMPLEX_EXPR, type,
  867. convert (subtype, TREE_OPERAND (expr, 0)),
  868. convert (subtype, TREE_OPERAND (expr, 1)));
  869. else
  870. {
  871. expr = save_expr (expr);
  872. return
  873. fold_build2 (COMPLEX_EXPR, type,
  874. convert (subtype,
  875. fold_build1 (REALPART_EXPR,
  876. TREE_TYPE (TREE_TYPE (expr)),
  877. expr)),
  878. convert (subtype,
  879. fold_build1 (IMAGPART_EXPR,
  880. TREE_TYPE (TREE_TYPE (expr)),
  881. expr)));
  882. }
  883. }
  884. case POINTER_TYPE:
  885. case REFERENCE_TYPE:
  886. error ("pointer value used where a complex was expected");
  887. return convert_to_complex (type, integer_zero_node);
  888. default:
  889. error ("aggregate value used where a complex was expected");
  890. return convert_to_complex (type, integer_zero_node);
  891. }
  892. }
  893. /* Convert EXPR to the vector type TYPE in the usual ways. */
  894. tree
  895. convert_to_vector (tree type, tree expr)
  896. {
  897. switch (TREE_CODE (TREE_TYPE (expr)))
  898. {
  899. case INTEGER_TYPE:
  900. case VECTOR_TYPE:
  901. if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
  902. {
  903. error ("can%'t convert between vector values of different size");
  904. return error_mark_node;
  905. }
  906. return build1 (VIEW_CONVERT_EXPR, type, expr);
  907. default:
  908. error ("can%'t convert value to a vector");
  909. return error_mark_node;
  910. }
  911. }
  912. /* Convert EXPR to some fixed-point type TYPE.
  913. EXPR must be fixed-point, float, integer, or enumeral;
  914. in other cases error is called. */
  915. tree
  916. convert_to_fixed (tree type, tree expr)
  917. {
  918. if (integer_zerop (expr))
  919. {
  920. tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
  921. return fixed_zero_node;
  922. }
  923. else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
  924. {
  925. tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
  926. return fixed_one_node;
  927. }
  928. switch (TREE_CODE (TREE_TYPE (expr)))
  929. {
  930. case FIXED_POINT_TYPE:
  931. case INTEGER_TYPE:
  932. case ENUMERAL_TYPE:
  933. case BOOLEAN_TYPE:
  934. case REAL_TYPE:
  935. return build1 (FIXED_CONVERT_EXPR, type, expr);
  936. case COMPLEX_TYPE:
  937. return convert (type,
  938. fold_build1 (REALPART_EXPR,
  939. TREE_TYPE (TREE_TYPE (expr)), expr));
  940. default:
  941. error ("aggregate value used where a fixed-point was expected");
  942. return error_mark_node;
  943. }
  944. }