internal-fn.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /* Internal functions.
  2. Copyright (C) 2011-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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "hash-set.h"
  19. #include "machmode.h"
  20. #include "vec.h"
  21. #include "double-int.h"
  22. #include "input.h"
  23. #include "alias.h"
  24. #include "symtab.h"
  25. #include "options.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "internal-fn.h"
  31. #include "stor-layout.h"
  32. #include "hashtab.h"
  33. #include "tm.h"
  34. #include "hard-reg-set.h"
  35. #include "function.h"
  36. #include "rtl.h"
  37. #include "flags.h"
  38. #include "statistics.h"
  39. #include "real.h"
  40. #include "fixed-value.h"
  41. #include "insn-config.h"
  42. #include "expmed.h"
  43. #include "dojump.h"
  44. #include "explow.h"
  45. #include "calls.h"
  46. #include "emit-rtl.h"
  47. #include "varasm.h"
  48. #include "stmt.h"
  49. #include "expr.h"
  50. #include "insn-codes.h"
  51. #include "optabs.h"
  52. #include "predict.h"
  53. #include "dominance.h"
  54. #include "cfg.h"
  55. #include "basic-block.h"
  56. #include "tree-ssa-alias.h"
  57. #include "gimple-expr.h"
  58. #include "is-a.h"
  59. #include "gimple.h"
  60. #include "ubsan.h"
  61. #include "target.h"
  62. #include "stringpool.h"
  63. #include "tree-ssanames.h"
  64. #include "diagnostic-core.h"
  65. /* The names of each internal function, indexed by function number. */
  66. const char *const internal_fn_name_array[] = {
  67. #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) #CODE,
  68. #include "internal-fn.def"
  69. #undef DEF_INTERNAL_FN
  70. "<invalid-fn>"
  71. };
  72. /* The ECF_* flags of each internal function, indexed by function number. */
  73. const int internal_fn_flags_array[] = {
  74. #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) FLAGS,
  75. #include "internal-fn.def"
  76. #undef DEF_INTERNAL_FN
  77. 0
  78. };
  79. /* Fnspec of each internal function, indexed by function number. */
  80. const_tree internal_fn_fnspec_array[IFN_LAST + 1];
  81. void
  82. init_internal_fns ()
  83. {
  84. #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
  85. if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \
  86. build_string ((int) sizeof (FNSPEC), FNSPEC ? FNSPEC : "");
  87. #include "internal-fn.def"
  88. #undef DEF_INTERNAL_FN
  89. internal_fn_fnspec_array[IFN_LAST] = 0;
  90. }
  91. /* ARRAY_TYPE is an array of vector modes. Return the associated insn
  92. for load-lanes-style optab OPTAB. The insn must exist. */
  93. static enum insn_code
  94. get_multi_vector_move (tree array_type, convert_optab optab)
  95. {
  96. enum insn_code icode;
  97. machine_mode imode;
  98. machine_mode vmode;
  99. gcc_assert (TREE_CODE (array_type) == ARRAY_TYPE);
  100. imode = TYPE_MODE (array_type);
  101. vmode = TYPE_MODE (TREE_TYPE (array_type));
  102. icode = convert_optab_handler (optab, imode, vmode);
  103. gcc_assert (icode != CODE_FOR_nothing);
  104. return icode;
  105. }
  106. /* Expand LOAD_LANES call STMT. */
  107. static void
  108. expand_LOAD_LANES (gcall *stmt)
  109. {
  110. struct expand_operand ops[2];
  111. tree type, lhs, rhs;
  112. rtx target, mem;
  113. lhs = gimple_call_lhs (stmt);
  114. rhs = gimple_call_arg (stmt, 0);
  115. type = TREE_TYPE (lhs);
  116. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  117. mem = expand_normal (rhs);
  118. gcc_assert (MEM_P (mem));
  119. PUT_MODE (mem, TYPE_MODE (type));
  120. create_output_operand (&ops[0], target, TYPE_MODE (type));
  121. create_fixed_operand (&ops[1], mem);
  122. expand_insn (get_multi_vector_move (type, vec_load_lanes_optab), 2, ops);
  123. }
  124. /* Expand STORE_LANES call STMT. */
  125. static void
  126. expand_STORE_LANES (gcall *stmt)
  127. {
  128. struct expand_operand ops[2];
  129. tree type, lhs, rhs;
  130. rtx target, reg;
  131. lhs = gimple_call_lhs (stmt);
  132. rhs = gimple_call_arg (stmt, 0);
  133. type = TREE_TYPE (rhs);
  134. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  135. reg = expand_normal (rhs);
  136. gcc_assert (MEM_P (target));
  137. PUT_MODE (target, TYPE_MODE (type));
  138. create_fixed_operand (&ops[0], target);
  139. create_input_operand (&ops[1], reg, TYPE_MODE (type));
  140. expand_insn (get_multi_vector_move (type, vec_store_lanes_optab), 2, ops);
  141. }
  142. static void
  143. expand_ANNOTATE (gcall *)
  144. {
  145. gcc_unreachable ();
  146. }
  147. /* This should get expanded in adjust_simduid_builtins. */
  148. static void
  149. expand_GOMP_SIMD_LANE (gcall *)
  150. {
  151. gcc_unreachable ();
  152. }
  153. /* This should get expanded in adjust_simduid_builtins. */
  154. static void
  155. expand_GOMP_SIMD_VF (gcall *)
  156. {
  157. gcc_unreachable ();
  158. }
  159. /* This should get expanded in adjust_simduid_builtins. */
  160. static void
  161. expand_GOMP_SIMD_LAST_LANE (gcall *)
  162. {
  163. gcc_unreachable ();
  164. }
  165. /* This should get expanded in the sanopt pass. */
  166. static void
  167. expand_UBSAN_NULL (gcall *)
  168. {
  169. gcc_unreachable ();
  170. }
  171. /* This should get expanded in the sanopt pass. */
  172. static void
  173. expand_UBSAN_BOUNDS (gcall *)
  174. {
  175. gcc_unreachable ();
  176. }
  177. /* This should get expanded in the sanopt pass. */
  178. static void
  179. expand_UBSAN_VPTR (gcall *)
  180. {
  181. gcc_unreachable ();
  182. }
  183. /* This should get expanded in the sanopt pass. */
  184. static void
  185. expand_UBSAN_OBJECT_SIZE (gcall *)
  186. {
  187. gcc_unreachable ();
  188. }
  189. /* This should get expanded in the sanopt pass. */
  190. static void
  191. expand_ASAN_CHECK (gcall *)
  192. {
  193. gcc_unreachable ();
  194. }
  195. /* This should get expanded in the tsan pass. */
  196. static void
  197. expand_TSAN_FUNC_EXIT (gcall *)
  198. {
  199. gcc_unreachable ();
  200. }
  201. /* Helper function for expand_addsub_overflow. Return 1
  202. if ARG interpreted as signed in its precision is known to be always
  203. positive or 2 if ARG is known to be always negative, or 3 if ARG may
  204. be positive or negative. */
  205. static int
  206. get_range_pos_neg (tree arg)
  207. {
  208. if (arg == error_mark_node)
  209. return 3;
  210. int prec = TYPE_PRECISION (TREE_TYPE (arg));
  211. int cnt = 0;
  212. if (TREE_CODE (arg) == INTEGER_CST)
  213. {
  214. wide_int w = wi::sext (arg, prec);
  215. if (wi::neg_p (w))
  216. return 2;
  217. else
  218. return 1;
  219. }
  220. while (CONVERT_EXPR_P (arg)
  221. && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
  222. && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
  223. {
  224. arg = TREE_OPERAND (arg, 0);
  225. /* Narrower value zero extended into wider type
  226. will always result in positive values. */
  227. if (TYPE_UNSIGNED (TREE_TYPE (arg))
  228. && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
  229. return 1;
  230. prec = TYPE_PRECISION (TREE_TYPE (arg));
  231. if (++cnt > 30)
  232. return 3;
  233. }
  234. if (TREE_CODE (arg) != SSA_NAME)
  235. return 3;
  236. wide_int arg_min, arg_max;
  237. while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
  238. {
  239. gimple g = SSA_NAME_DEF_STMT (arg);
  240. if (is_gimple_assign (g)
  241. && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
  242. {
  243. tree t = gimple_assign_rhs1 (g);
  244. if (INTEGRAL_TYPE_P (TREE_TYPE (t))
  245. && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
  246. {
  247. if (TYPE_UNSIGNED (TREE_TYPE (t))
  248. && TYPE_PRECISION (TREE_TYPE (t)) < prec)
  249. return 1;
  250. prec = TYPE_PRECISION (TREE_TYPE (t));
  251. arg = t;
  252. if (++cnt > 30)
  253. return 3;
  254. continue;
  255. }
  256. }
  257. return 3;
  258. }
  259. if (TYPE_UNSIGNED (TREE_TYPE (arg)))
  260. {
  261. /* For unsigned values, the "positive" range comes
  262. below the "negative" range. */
  263. if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED))
  264. return 1;
  265. if (wi::neg_p (wi::sext (arg_min, prec), SIGNED))
  266. return 2;
  267. }
  268. else
  269. {
  270. if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED))
  271. return 1;
  272. if (wi::neg_p (wi::sext (arg_max, prec), SIGNED))
  273. return 2;
  274. }
  275. return 3;
  276. }
  277. /* Return minimum precision needed to represent all values
  278. of ARG in SIGNed integral type. */
  279. static int
  280. get_min_precision (tree arg, signop sign)
  281. {
  282. int prec = TYPE_PRECISION (TREE_TYPE (arg));
  283. int cnt = 0;
  284. signop orig_sign = sign;
  285. if (TREE_CODE (arg) == INTEGER_CST)
  286. {
  287. int p;
  288. if (TYPE_SIGN (TREE_TYPE (arg)) != sign)
  289. {
  290. widest_int w = wi::to_widest (arg);
  291. w = wi::ext (w, prec, sign);
  292. p = wi::min_precision (w, sign);
  293. }
  294. else
  295. p = wi::min_precision (arg, sign);
  296. return MIN (p, prec);
  297. }
  298. while (CONVERT_EXPR_P (arg)
  299. && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
  300. && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
  301. {
  302. arg = TREE_OPERAND (arg, 0);
  303. if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
  304. {
  305. if (TYPE_UNSIGNED (TREE_TYPE (arg)))
  306. sign = UNSIGNED;
  307. else if (sign == UNSIGNED && get_range_pos_neg (arg) != 1)
  308. return prec + (orig_sign != sign);
  309. prec = TYPE_PRECISION (TREE_TYPE (arg));
  310. }
  311. if (++cnt > 30)
  312. return prec + (orig_sign != sign);
  313. }
  314. if (TREE_CODE (arg) != SSA_NAME)
  315. return prec + (orig_sign != sign);
  316. wide_int arg_min, arg_max;
  317. while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
  318. {
  319. gimple g = SSA_NAME_DEF_STMT (arg);
  320. if (is_gimple_assign (g)
  321. && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
  322. {
  323. tree t = gimple_assign_rhs1 (g);
  324. if (INTEGRAL_TYPE_P (TREE_TYPE (t))
  325. && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
  326. {
  327. arg = t;
  328. if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
  329. {
  330. if (TYPE_UNSIGNED (TREE_TYPE (arg)))
  331. sign = UNSIGNED;
  332. else if (sign == UNSIGNED && get_range_pos_neg (arg) != 1)
  333. return prec + (orig_sign != sign);
  334. prec = TYPE_PRECISION (TREE_TYPE (arg));
  335. }
  336. if (++cnt > 30)
  337. return prec + (orig_sign != sign);
  338. continue;
  339. }
  340. }
  341. return prec + (orig_sign != sign);
  342. }
  343. if (sign == TYPE_SIGN (TREE_TYPE (arg)))
  344. {
  345. int p1 = wi::min_precision (arg_min, sign);
  346. int p2 = wi::min_precision (arg_max, sign);
  347. p1 = MAX (p1, p2);
  348. prec = MIN (prec, p1);
  349. }
  350. else if (sign == UNSIGNED && !wi::neg_p (arg_min, SIGNED))
  351. {
  352. int p = wi::min_precision (arg_max, SIGNED);
  353. prec = MIN (prec, p);
  354. }
  355. return prec + (orig_sign != sign);
  356. }
  357. /* Helper for expand_*_overflow. Store RES into the __real__ part
  358. of TARGET. If RES has larger MODE than __real__ part of TARGET,
  359. set the __imag__ part to 1 if RES doesn't fit into it. */
  360. static void
  361. expand_arith_overflow_result_store (tree lhs, rtx target,
  362. machine_mode mode, rtx res)
  363. {
  364. machine_mode tgtmode = GET_MODE_INNER (GET_MODE (target));
  365. rtx lres = res;
  366. if (tgtmode != mode)
  367. {
  368. rtx_code_label *done_label = gen_label_rtx ();
  369. int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
  370. lres = convert_modes (tgtmode, mode, res, uns);
  371. gcc_assert (GET_MODE_PRECISION (tgtmode) < GET_MODE_PRECISION (mode));
  372. do_compare_rtx_and_jump (res, convert_modes (mode, tgtmode, lres, uns),
  373. EQ, true, mode, NULL_RTX, NULL_RTX, done_label,
  374. PROB_VERY_LIKELY);
  375. write_complex_part (target, const1_rtx, true);
  376. emit_label (done_label);
  377. }
  378. write_complex_part (target, lres, false);
  379. }
  380. /* Helper for expand_*_overflow. Store RES into TARGET. */
  381. static void
  382. expand_ubsan_result_store (rtx target, rtx res)
  383. {
  384. if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
  385. /* If this is a scalar in a register that is stored in a wider mode
  386. than the declared mode, compute the result into its declared mode
  387. and then convert to the wider mode. Our value is the computed
  388. expression. */
  389. convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
  390. else
  391. emit_move_insn (target, res);
  392. }
  393. /* Add sub/add overflow checking to the statement STMT.
  394. CODE says whether the operation is +, or -. */
  395. static void
  396. expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
  397. tree arg0, tree arg1, bool unsr_p, bool uns0_p,
  398. bool uns1_p, bool is_ubsan)
  399. {
  400. rtx res, target = NULL_RTX;
  401. tree fn;
  402. rtx_code_label *done_label = gen_label_rtx ();
  403. rtx_code_label *do_error = gen_label_rtx ();
  404. do_pending_stack_adjust ();
  405. rtx op0 = expand_normal (arg0);
  406. rtx op1 = expand_normal (arg1);
  407. machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
  408. int prec = GET_MODE_PRECISION (mode);
  409. rtx sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
  410. bool do_xor = false;
  411. if (is_ubsan)
  412. gcc_assert (!unsr_p && !uns0_p && !uns1_p);
  413. if (lhs)
  414. {
  415. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  416. if (!is_ubsan)
  417. write_complex_part (target, const0_rtx, true);
  418. }
  419. /* We assume both operands and result have the same precision
  420. here (GET_MODE_BITSIZE (mode)), S stands for signed type
  421. with that precision, U for unsigned type with that precision,
  422. sgn for unsigned most significant bit in that precision.
  423. s1 is signed first operand, u1 is unsigned first operand,
  424. s2 is signed second operand, u2 is unsigned second operand,
  425. sr is signed result, ur is unsigned result and the following
  426. rules say how to compute result (which is always result of
  427. the operands as if both were unsigned, cast to the right
  428. signedness) and how to compute whether operation overflowed.
  429. s1 + s2 -> sr
  430. res = (S) ((U) s1 + (U) s2)
  431. ovf = s2 < 0 ? res > s1 : res < s1 (or jump on overflow)
  432. s1 - s2 -> sr
  433. res = (S) ((U) s1 - (U) s2)
  434. ovf = s2 < 0 ? res < s1 : res > s2 (or jump on overflow)
  435. u1 + u2 -> ur
  436. res = u1 + u2
  437. ovf = res < u1 (or jump on carry, but RTL opts will handle it)
  438. u1 - u2 -> ur
  439. res = u1 - u2
  440. ovf = res > u1 (or jump on carry, but RTL opts will handle it)
  441. s1 + u2 -> sr
  442. res = (S) ((U) s1 + u2)
  443. ovf = ((U) res ^ sgn) < u2
  444. s1 + u2 -> ur
  445. t1 = (S) (u2 ^ sgn)
  446. t2 = s1 + t1
  447. res = (U) t2 ^ sgn
  448. ovf = t1 < 0 ? t2 > s1 : t2 < s1 (or jump on overflow)
  449. s1 - u2 -> sr
  450. res = (S) ((U) s1 - u2)
  451. ovf = u2 > ((U) s1 ^ sgn)
  452. s1 - u2 -> ur
  453. res = (U) s1 - u2
  454. ovf = s1 < 0 || u2 > (U) s1
  455. u1 - s2 -> sr
  456. res = u1 - (U) s2
  457. ovf = u1 >= ((U) s2 ^ sgn)
  458. u1 - s2 -> ur
  459. t1 = u1 ^ sgn
  460. t2 = t1 - (U) s2
  461. res = t2 ^ sgn
  462. ovf = s2 < 0 ? (S) t2 < (S) t1 : (S) t2 > (S) t1 (or jump on overflow)
  463. s1 + s2 -> ur
  464. res = (U) s1 + (U) s2
  465. ovf = s2 < 0 ? (s1 | (S) res) < 0) : (s1 & (S) res) < 0)
  466. u1 + u2 -> sr
  467. res = (S) (u1 + u2)
  468. ovf = (U) res < u2 || res < 0
  469. u1 - u2 -> sr
  470. res = (S) (u1 - u2)
  471. ovf = u1 >= u2 ? res < 0 : res >= 0
  472. s1 - s2 -> ur
  473. res = (U) s1 - (U) s2
  474. ovf = s2 >= 0 ? ((s1 | (S) res) < 0) : ((s1 & (S) res) < 0) */
  475. if (code == PLUS_EXPR && uns0_p && !uns1_p)
  476. {
  477. /* PLUS_EXPR is commutative, if operand signedness differs,
  478. canonicalize to the first operand being signed and second
  479. unsigned to simplify following code. */
  480. rtx tem = op1;
  481. op1 = op0;
  482. op0 = tem;
  483. tree t = arg1;
  484. arg1 = arg0;
  485. arg0 = t;
  486. uns0_p = 0;
  487. uns1_p = 1;
  488. }
  489. /* u1 +- u2 -> ur */
  490. if (uns0_p && uns1_p && unsr_p)
  491. {
  492. /* Compute the operation. On RTL level, the addition is always
  493. unsigned. */
  494. res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
  495. op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
  496. rtx tem = op0;
  497. /* For PLUS_EXPR, the operation is commutative, so we can pick
  498. operand to compare against. For prec <= BITS_PER_WORD, I think
  499. preferring REG operand is better over CONST_INT, because
  500. the CONST_INT might enlarge the instruction or CSE would need
  501. to figure out we'd already loaded it into a register before.
  502. For prec > BITS_PER_WORD, I think CONST_INT might be more beneficial,
  503. as then the multi-word comparison can be perhaps simplified. */
  504. if (code == PLUS_EXPR
  505. && (prec <= BITS_PER_WORD
  506. ? (CONST_SCALAR_INT_P (op0) && REG_P (op1))
  507. : CONST_SCALAR_INT_P (op1)))
  508. tem = op1;
  509. do_compare_rtx_and_jump (res, tem, code == PLUS_EXPR ? GEU : LEU,
  510. true, mode, NULL_RTX, NULL_RTX, done_label,
  511. PROB_VERY_LIKELY);
  512. goto do_error_label;
  513. }
  514. /* s1 +- u2 -> sr */
  515. if (!uns0_p && uns1_p && !unsr_p)
  516. {
  517. /* Compute the operation. On RTL level, the addition is always
  518. unsigned. */
  519. res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
  520. op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
  521. rtx tem = expand_binop (mode, add_optab,
  522. code == PLUS_EXPR ? res : op0, sgn,
  523. NULL_RTX, false, OPTAB_LIB_WIDEN);
  524. do_compare_rtx_and_jump (tem, op1, GEU, true, mode, NULL_RTX, NULL_RTX,
  525. done_label, PROB_VERY_LIKELY);
  526. goto do_error_label;
  527. }
  528. /* s1 + u2 -> ur */
  529. if (code == PLUS_EXPR && !uns0_p && uns1_p && unsr_p)
  530. {
  531. op1 = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
  532. OPTAB_LIB_WIDEN);
  533. /* As we've changed op1, we have to avoid using the value range
  534. for the original argument. */
  535. arg1 = error_mark_node;
  536. do_xor = true;
  537. goto do_signed;
  538. }
  539. /* u1 - s2 -> ur */
  540. if (code == MINUS_EXPR && uns0_p && !uns1_p && unsr_p)
  541. {
  542. op0 = expand_binop (mode, add_optab, op0, sgn, NULL_RTX, false,
  543. OPTAB_LIB_WIDEN);
  544. /* As we've changed op0, we have to avoid using the value range
  545. for the original argument. */
  546. arg0 = error_mark_node;
  547. do_xor = true;
  548. goto do_signed;
  549. }
  550. /* s1 - u2 -> ur */
  551. if (code == MINUS_EXPR && !uns0_p && uns1_p && unsr_p)
  552. {
  553. /* Compute the operation. On RTL level, the addition is always
  554. unsigned. */
  555. res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
  556. OPTAB_LIB_WIDEN);
  557. int pos_neg = get_range_pos_neg (arg0);
  558. if (pos_neg == 2)
  559. /* If ARG0 is known to be always negative, this is always overflow. */
  560. emit_jump (do_error);
  561. else if (pos_neg == 3)
  562. /* If ARG0 is not known to be always positive, check at runtime. */
  563. do_compare_rtx_and_jump (op0, const0_rtx, LT, false, mode, NULL_RTX,
  564. NULL_RTX, do_error, PROB_VERY_UNLIKELY);
  565. do_compare_rtx_and_jump (op1, op0, LEU, true, mode, NULL_RTX, NULL_RTX,
  566. done_label, PROB_VERY_LIKELY);
  567. goto do_error_label;
  568. }
  569. /* u1 - s2 -> sr */
  570. if (code == MINUS_EXPR && uns0_p && !uns1_p && !unsr_p)
  571. {
  572. /* Compute the operation. On RTL level, the addition is always
  573. unsigned. */
  574. res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
  575. OPTAB_LIB_WIDEN);
  576. rtx tem = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
  577. OPTAB_LIB_WIDEN);
  578. do_compare_rtx_and_jump (op0, tem, LTU, true, mode, NULL_RTX, NULL_RTX,
  579. done_label, PROB_VERY_LIKELY);
  580. goto do_error_label;
  581. }
  582. /* u1 + u2 -> sr */
  583. if (code == PLUS_EXPR && uns0_p && uns1_p && !unsr_p)
  584. {
  585. /* Compute the operation. On RTL level, the addition is always
  586. unsigned. */
  587. res = expand_binop (mode, add_optab, op0, op1, NULL_RTX, false,
  588. OPTAB_LIB_WIDEN);
  589. do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
  590. NULL_RTX, do_error, PROB_VERY_UNLIKELY);
  591. rtx tem = op1;
  592. /* The operation is commutative, so we can pick operand to compare
  593. against. For prec <= BITS_PER_WORD, I think preferring REG operand
  594. is better over CONST_INT, because the CONST_INT might enlarge the
  595. instruction or CSE would need to figure out we'd already loaded it
  596. into a register before. For prec > BITS_PER_WORD, I think CONST_INT
  597. might be more beneficial, as then the multi-word comparison can be
  598. perhaps simplified. */
  599. if (prec <= BITS_PER_WORD
  600. ? (CONST_SCALAR_INT_P (op1) && REG_P (op0))
  601. : CONST_SCALAR_INT_P (op0))
  602. tem = op0;
  603. do_compare_rtx_and_jump (res, tem, GEU, true, mode, NULL_RTX, NULL_RTX,
  604. done_label, PROB_VERY_LIKELY);
  605. goto do_error_label;
  606. }
  607. /* s1 +- s2 -> ur */
  608. if (!uns0_p && !uns1_p && unsr_p)
  609. {
  610. /* Compute the operation. On RTL level, the addition is always
  611. unsigned. */
  612. res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
  613. op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
  614. int pos_neg = get_range_pos_neg (arg1);
  615. if (code == PLUS_EXPR)
  616. {
  617. int pos_neg0 = get_range_pos_neg (arg0);
  618. if (pos_neg0 != 3 && pos_neg == 3)
  619. {
  620. rtx tem = op1;
  621. op1 = op0;
  622. op0 = tem;
  623. pos_neg = pos_neg0;
  624. }
  625. }
  626. rtx tem;
  627. if (pos_neg != 3)
  628. {
  629. tem = expand_binop (mode, ((pos_neg == 1) ^ (code == MINUS_EXPR))
  630. ? and_optab : ior_optab,
  631. op0, res, NULL_RTX, false, OPTAB_LIB_WIDEN);
  632. do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
  633. NULL_RTX, done_label, PROB_VERY_LIKELY);
  634. }
  635. else
  636. {
  637. rtx_code_label *do_ior_label = gen_label_rtx ();
  638. do_compare_rtx_and_jump (op1, const0_rtx,
  639. code == MINUS_EXPR ? GE : LT, false, mode,
  640. NULL_RTX, NULL_RTX, do_ior_label,
  641. PROB_EVEN);
  642. tem = expand_binop (mode, and_optab, op0, res, NULL_RTX, false,
  643. OPTAB_LIB_WIDEN);
  644. do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
  645. NULL_RTX, done_label, PROB_VERY_LIKELY);
  646. emit_jump (do_error);
  647. emit_label (do_ior_label);
  648. tem = expand_binop (mode, ior_optab, op0, res, NULL_RTX, false,
  649. OPTAB_LIB_WIDEN);
  650. do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
  651. NULL_RTX, done_label, PROB_VERY_LIKELY);
  652. }
  653. goto do_error_label;
  654. }
  655. /* u1 - u2 -> sr */
  656. if (code == MINUS_EXPR && uns0_p && uns1_p && !unsr_p)
  657. {
  658. /* Compute the operation. On RTL level, the addition is always
  659. unsigned. */
  660. res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
  661. OPTAB_LIB_WIDEN);
  662. rtx_code_label *op0_geu_op1 = gen_label_rtx ();
  663. do_compare_rtx_and_jump (op0, op1, GEU, true, mode, NULL_RTX, NULL_RTX,
  664. op0_geu_op1, PROB_EVEN);
  665. do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
  666. NULL_RTX, done_label, PROB_VERY_LIKELY);
  667. emit_jump (do_error);
  668. emit_label (op0_geu_op1);
  669. do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
  670. NULL_RTX, done_label, PROB_VERY_LIKELY);
  671. goto do_error_label;
  672. }
  673. gcc_assert (!uns0_p && !uns1_p && !unsr_p);
  674. /* s1 +- s2 -> sr */
  675. do_signed: ;
  676. enum insn_code icode;
  677. icode = optab_handler (code == PLUS_EXPR ? addv4_optab : subv4_optab, mode);
  678. if (icode != CODE_FOR_nothing)
  679. {
  680. struct expand_operand ops[4];
  681. rtx_insn *last = get_last_insn ();
  682. res = gen_reg_rtx (mode);
  683. create_output_operand (&ops[0], res, mode);
  684. create_input_operand (&ops[1], op0, mode);
  685. create_input_operand (&ops[2], op1, mode);
  686. create_fixed_operand (&ops[3], do_error);
  687. if (maybe_expand_insn (icode, 4, ops))
  688. {
  689. last = get_last_insn ();
  690. if (profile_status_for_fn (cfun) != PROFILE_ABSENT
  691. && JUMP_P (last)
  692. && any_condjump_p (last)
  693. && !find_reg_note (last, REG_BR_PROB, 0))
  694. add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
  695. emit_jump (done_label);
  696. }
  697. else
  698. {
  699. delete_insns_since (last);
  700. icode = CODE_FOR_nothing;
  701. }
  702. }
  703. if (icode == CODE_FOR_nothing)
  704. {
  705. rtx_code_label *sub_check = gen_label_rtx ();
  706. int pos_neg = 3;
  707. /* Compute the operation. On RTL level, the addition is always
  708. unsigned. */
  709. res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
  710. op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
  711. /* If we can prove one of the arguments (for MINUS_EXPR only
  712. the second operand, as subtraction is not commutative) is always
  713. non-negative or always negative, we can do just one comparison
  714. and conditional jump instead of 2 at runtime, 3 present in the
  715. emitted code. If one of the arguments is CONST_INT, all we
  716. need is to make sure it is op1, then the first
  717. do_compare_rtx_and_jump will be just folded. Otherwise try
  718. to use range info if available. */
  719. if (code == PLUS_EXPR && CONST_INT_P (op0))
  720. {
  721. rtx tem = op0;
  722. op0 = op1;
  723. op1 = tem;
  724. }
  725. else if (CONST_INT_P (op1))
  726. ;
  727. else if (code == PLUS_EXPR && TREE_CODE (arg0) == SSA_NAME)
  728. {
  729. pos_neg = get_range_pos_neg (arg0);
  730. if (pos_neg != 3)
  731. {
  732. rtx tem = op0;
  733. op0 = op1;
  734. op1 = tem;
  735. }
  736. }
  737. if (pos_neg == 3 && !CONST_INT_P (op1) && TREE_CODE (arg1) == SSA_NAME)
  738. pos_neg = get_range_pos_neg (arg1);
  739. /* If the op1 is negative, we have to use a different check. */
  740. if (pos_neg == 3)
  741. do_compare_rtx_and_jump (op1, const0_rtx, LT, false, mode, NULL_RTX,
  742. NULL_RTX, sub_check, PROB_EVEN);
  743. /* Compare the result of the operation with one of the operands. */
  744. if (pos_neg & 1)
  745. do_compare_rtx_and_jump (res, op0, code == PLUS_EXPR ? GE : LE,
  746. false, mode, NULL_RTX, NULL_RTX, done_label,
  747. PROB_VERY_LIKELY);
  748. /* If we get here, we have to print the error. */
  749. if (pos_neg == 3)
  750. {
  751. emit_jump (do_error);
  752. emit_label (sub_check);
  753. }
  754. /* We have k = a + b for b < 0 here. k <= a must hold. */
  755. if (pos_neg & 2)
  756. do_compare_rtx_and_jump (res, op0, code == PLUS_EXPR ? LE : GE,
  757. false, mode, NULL_RTX, NULL_RTX, done_label,
  758. PROB_VERY_LIKELY);
  759. }
  760. do_error_label:
  761. emit_label (do_error);
  762. if (is_ubsan)
  763. {
  764. /* Expand the ubsan builtin call. */
  765. push_temp_slots ();
  766. fn = ubsan_build_overflow_builtin (code, loc, TREE_TYPE (arg0),
  767. arg0, arg1);
  768. expand_normal (fn);
  769. pop_temp_slots ();
  770. do_pending_stack_adjust ();
  771. }
  772. else if (lhs)
  773. write_complex_part (target, const1_rtx, true);
  774. /* We're done. */
  775. emit_label (done_label);
  776. if (lhs)
  777. {
  778. if (is_ubsan)
  779. expand_ubsan_result_store (target, res);
  780. else
  781. {
  782. if (do_xor)
  783. res = expand_binop (mode, add_optab, res, sgn, NULL_RTX, false,
  784. OPTAB_LIB_WIDEN);
  785. expand_arith_overflow_result_store (lhs, target, mode, res);
  786. }
  787. }
  788. }
  789. /* Add negate overflow checking to the statement STMT. */
  790. static void
  791. expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan)
  792. {
  793. rtx res, op1;
  794. tree fn;
  795. rtx_code_label *done_label, *do_error;
  796. rtx target = NULL_RTX;
  797. done_label = gen_label_rtx ();
  798. do_error = gen_label_rtx ();
  799. do_pending_stack_adjust ();
  800. op1 = expand_normal (arg1);
  801. machine_mode mode = TYPE_MODE (TREE_TYPE (arg1));
  802. if (lhs)
  803. {
  804. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  805. if (!is_ubsan)
  806. write_complex_part (target, const0_rtx, true);
  807. }
  808. enum insn_code icode = optab_handler (negv3_optab, mode);
  809. if (icode != CODE_FOR_nothing)
  810. {
  811. struct expand_operand ops[3];
  812. rtx_insn *last = get_last_insn ();
  813. res = gen_reg_rtx (mode);
  814. create_output_operand (&ops[0], res, mode);
  815. create_input_operand (&ops[1], op1, mode);
  816. create_fixed_operand (&ops[2], do_error);
  817. if (maybe_expand_insn (icode, 3, ops))
  818. {
  819. last = get_last_insn ();
  820. if (profile_status_for_fn (cfun) != PROFILE_ABSENT
  821. && JUMP_P (last)
  822. && any_condjump_p (last)
  823. && !find_reg_note (last, REG_BR_PROB, 0))
  824. add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
  825. emit_jump (done_label);
  826. }
  827. else
  828. {
  829. delete_insns_since (last);
  830. icode = CODE_FOR_nothing;
  831. }
  832. }
  833. if (icode == CODE_FOR_nothing)
  834. {
  835. /* Compute the operation. On RTL level, the addition is always
  836. unsigned. */
  837. res = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
  838. /* Compare the operand with the most negative value. */
  839. rtx minv = expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1)));
  840. do_compare_rtx_and_jump (op1, minv, NE, true, mode, NULL_RTX, NULL_RTX,
  841. done_label, PROB_VERY_LIKELY);
  842. }
  843. emit_label (do_error);
  844. if (is_ubsan)
  845. {
  846. /* Expand the ubsan builtin call. */
  847. push_temp_slots ();
  848. fn = ubsan_build_overflow_builtin (NEGATE_EXPR, loc, TREE_TYPE (arg1),
  849. arg1, NULL_TREE);
  850. expand_normal (fn);
  851. pop_temp_slots ();
  852. do_pending_stack_adjust ();
  853. }
  854. else if (lhs)
  855. write_complex_part (target, const1_rtx, true);
  856. /* We're done. */
  857. emit_label (done_label);
  858. if (lhs)
  859. {
  860. if (is_ubsan)
  861. expand_ubsan_result_store (target, res);
  862. else
  863. expand_arith_overflow_result_store (lhs, target, mode, res);
  864. }
  865. }
  866. /* Add mul overflow checking to the statement STMT. */
  867. static void
  868. expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
  869. bool unsr_p, bool uns0_p, bool uns1_p, bool is_ubsan)
  870. {
  871. rtx res, op0, op1;
  872. tree fn, type;
  873. rtx_code_label *done_label, *do_error;
  874. rtx target = NULL_RTX;
  875. signop sign;
  876. enum insn_code icode;
  877. done_label = gen_label_rtx ();
  878. do_error = gen_label_rtx ();
  879. do_pending_stack_adjust ();
  880. op0 = expand_normal (arg0);
  881. op1 = expand_normal (arg1);
  882. machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
  883. bool uns = unsr_p;
  884. if (lhs)
  885. {
  886. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  887. if (!is_ubsan)
  888. write_complex_part (target, const0_rtx, true);
  889. }
  890. if (is_ubsan)
  891. gcc_assert (!unsr_p && !uns0_p && !uns1_p);
  892. /* We assume both operands and result have the same precision
  893. here (GET_MODE_BITSIZE (mode)), S stands for signed type
  894. with that precision, U for unsigned type with that precision,
  895. sgn for unsigned most significant bit in that precision.
  896. s1 is signed first operand, u1 is unsigned first operand,
  897. s2 is signed second operand, u2 is unsigned second operand,
  898. sr is signed result, ur is unsigned result and the following
  899. rules say how to compute result (which is always result of
  900. the operands as if both were unsigned, cast to the right
  901. signedness) and how to compute whether operation overflowed.
  902. main_ovf (false) stands for jump on signed multiplication
  903. overflow or the main algorithm with uns == false.
  904. main_ovf (true) stands for jump on unsigned multiplication
  905. overflow or the main algorithm with uns == true.
  906. s1 * s2 -> sr
  907. res = (S) ((U) s1 * (U) s2)
  908. ovf = main_ovf (false)
  909. u1 * u2 -> ur
  910. res = u1 * u2
  911. ovf = main_ovf (true)
  912. s1 * u2 -> ur
  913. res = (U) s1 * u2
  914. ovf = (s1 < 0 && u2) || main_ovf (true)
  915. u1 * u2 -> sr
  916. res = (S) (u1 * u2)
  917. ovf = res < 0 || main_ovf (true)
  918. s1 * u2 -> sr
  919. res = (S) ((U) s1 * u2)
  920. ovf = (S) u2 >= 0 ? main_ovf (false)
  921. : (s1 != 0 && (s1 != -1 || u2 != (U) res))
  922. s1 * s2 -> ur
  923. t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1)
  924. t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2)
  925. res = t1 * t2
  926. ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true) */
  927. if (uns0_p && !uns1_p)
  928. {
  929. /* Multiplication is commutative, if operand signedness differs,
  930. canonicalize to the first operand being signed and second
  931. unsigned to simplify following code. */
  932. rtx tem = op1;
  933. op1 = op0;
  934. op0 = tem;
  935. tree t = arg1;
  936. arg1 = arg0;
  937. arg0 = t;
  938. uns0_p = 0;
  939. uns1_p = 1;
  940. }
  941. int pos_neg0 = get_range_pos_neg (arg0);
  942. int pos_neg1 = get_range_pos_neg (arg1);
  943. /* s1 * u2 -> ur */
  944. if (!uns0_p && uns1_p && unsr_p)
  945. {
  946. switch (pos_neg0)
  947. {
  948. case 1:
  949. /* If s1 is non-negative, just perform normal u1 * u2 -> ur. */
  950. goto do_main;
  951. case 2:
  952. /* If s1 is negative, avoid the main code, just multiply and
  953. signal overflow if op1 is not 0. */
  954. struct separate_ops ops;
  955. ops.code = MULT_EXPR;
  956. ops.type = TREE_TYPE (arg1);
  957. ops.op0 = make_tree (ops.type, op0);
  958. ops.op1 = make_tree (ops.type, op1);
  959. ops.op2 = NULL_TREE;
  960. ops.location = loc;
  961. res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  962. do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
  963. NULL_RTX, done_label, PROB_VERY_LIKELY);
  964. goto do_error_label;
  965. case 3:
  966. rtx_code_label *do_main_label;
  967. do_main_label = gen_label_rtx ();
  968. do_compare_rtx_and_jump (op0, const0_rtx, GE, false, mode, NULL_RTX,
  969. NULL_RTX, do_main_label, PROB_VERY_LIKELY);
  970. do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
  971. NULL_RTX, do_main_label, PROB_VERY_LIKELY);
  972. write_complex_part (target, const1_rtx, true);
  973. emit_label (do_main_label);
  974. goto do_main;
  975. default:
  976. gcc_unreachable ();
  977. }
  978. }
  979. /* u1 * u2 -> sr */
  980. if (uns0_p && uns1_p && !unsr_p)
  981. {
  982. uns = true;
  983. /* Rest of handling of this case after res is computed. */
  984. goto do_main;
  985. }
  986. /* s1 * u2 -> sr */
  987. if (!uns0_p && uns1_p && !unsr_p)
  988. {
  989. switch (pos_neg1)
  990. {
  991. case 1:
  992. goto do_main;
  993. case 2:
  994. /* If (S) u2 is negative (i.e. u2 is larger than maximum of S,
  995. avoid the main code, just multiply and signal overflow
  996. unless 0 * u2 or -1 * ((U) Smin). */
  997. struct separate_ops ops;
  998. ops.code = MULT_EXPR;
  999. ops.type = TREE_TYPE (arg1);
  1000. ops.op0 = make_tree (ops.type, op0);
  1001. ops.op1 = make_tree (ops.type, op1);
  1002. ops.op2 = NULL_TREE;
  1003. ops.location = loc;
  1004. res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1005. do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
  1006. NULL_RTX, done_label, PROB_VERY_LIKELY);
  1007. do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
  1008. NULL_RTX, do_error, PROB_VERY_UNLIKELY);
  1009. int prec;
  1010. prec = GET_MODE_PRECISION (mode);
  1011. rtx sgn;
  1012. sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
  1013. do_compare_rtx_and_jump (op1, sgn, EQ, true, mode, NULL_RTX,
  1014. NULL_RTX, done_label, PROB_VERY_LIKELY);
  1015. goto do_error_label;
  1016. case 3:
  1017. /* Rest of handling of this case after res is computed. */
  1018. goto do_main;
  1019. default:
  1020. gcc_unreachable ();
  1021. }
  1022. }
  1023. /* s1 * s2 -> ur */
  1024. if (!uns0_p && !uns1_p && unsr_p)
  1025. {
  1026. rtx tem, tem2;
  1027. switch (pos_neg0 | pos_neg1)
  1028. {
  1029. case 1: /* Both operands known to be non-negative. */
  1030. goto do_main;
  1031. case 2: /* Both operands known to be negative. */
  1032. op0 = expand_unop (mode, neg_optab, op0, NULL_RTX, false);
  1033. op1 = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
  1034. /* Avoid looking at arg0/arg1 ranges, as we've changed
  1035. the arguments. */
  1036. arg0 = error_mark_node;
  1037. arg1 = error_mark_node;
  1038. goto do_main;
  1039. case 3:
  1040. if ((pos_neg0 ^ pos_neg1) == 3)
  1041. {
  1042. /* If one operand is known to be negative and the other
  1043. non-negative, this overflows always, unless the non-negative
  1044. one is 0. Just do normal multiply and set overflow
  1045. unless one of the operands is 0. */
  1046. struct separate_ops ops;
  1047. ops.code = MULT_EXPR;
  1048. ops.type
  1049. = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
  1050. 1);
  1051. ops.op0 = make_tree (ops.type, op0);
  1052. ops.op1 = make_tree (ops.type, op1);
  1053. ops.op2 = NULL_TREE;
  1054. ops.location = loc;
  1055. res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1056. tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
  1057. OPTAB_LIB_WIDEN);
  1058. do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode,
  1059. NULL_RTX, NULL_RTX, done_label,
  1060. PROB_VERY_LIKELY);
  1061. goto do_error_label;
  1062. }
  1063. /* The general case, do all the needed comparisons at runtime. */
  1064. rtx_code_label *do_main_label, *after_negate_label;
  1065. rtx rop0, rop1;
  1066. rop0 = gen_reg_rtx (mode);
  1067. rop1 = gen_reg_rtx (mode);
  1068. emit_move_insn (rop0, op0);
  1069. emit_move_insn (rop1, op1);
  1070. op0 = rop0;
  1071. op1 = rop1;
  1072. do_main_label = gen_label_rtx ();
  1073. after_negate_label = gen_label_rtx ();
  1074. tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
  1075. OPTAB_LIB_WIDEN);
  1076. do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
  1077. NULL_RTX, after_negate_label,
  1078. PROB_VERY_LIKELY);
  1079. /* Both arguments negative here, negate them and continue with
  1080. normal unsigned overflow checking multiplication. */
  1081. emit_move_insn (op0, expand_unop (mode, neg_optab, op0,
  1082. NULL_RTX, false));
  1083. emit_move_insn (op1, expand_unop (mode, neg_optab, op1,
  1084. NULL_RTX, false));
  1085. /* Avoid looking at arg0/arg1 ranges, as we might have changed
  1086. the arguments. */
  1087. arg0 = error_mark_node;
  1088. arg1 = error_mark_node;
  1089. emit_jump (do_main_label);
  1090. emit_label (after_negate_label);
  1091. tem2 = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
  1092. OPTAB_LIB_WIDEN);
  1093. do_compare_rtx_and_jump (tem2, const0_rtx, GE, false, mode, NULL_RTX,
  1094. NULL_RTX, do_main_label, PROB_VERY_LIKELY);
  1095. /* One argument is negative here, the other positive. This
  1096. overflows always, unless one of the arguments is 0. But
  1097. if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1
  1098. is, thus we can keep do_main code oring in overflow as is. */
  1099. do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, NULL_RTX,
  1100. NULL_RTX, do_main_label, PROB_VERY_LIKELY);
  1101. write_complex_part (target, const1_rtx, true);
  1102. emit_label (do_main_label);
  1103. goto do_main;
  1104. default:
  1105. gcc_unreachable ();
  1106. }
  1107. }
  1108. do_main:
  1109. type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), uns);
  1110. sign = uns ? UNSIGNED : SIGNED;
  1111. icode = optab_handler (uns ? umulv4_optab : mulv4_optab, mode);
  1112. if (icode != CODE_FOR_nothing)
  1113. {
  1114. struct expand_operand ops[4];
  1115. rtx_insn *last = get_last_insn ();
  1116. res = gen_reg_rtx (mode);
  1117. create_output_operand (&ops[0], res, mode);
  1118. create_input_operand (&ops[1], op0, mode);
  1119. create_input_operand (&ops[2], op1, mode);
  1120. create_fixed_operand (&ops[3], do_error);
  1121. if (maybe_expand_insn (icode, 4, ops))
  1122. {
  1123. last = get_last_insn ();
  1124. if (profile_status_for_fn (cfun) != PROFILE_ABSENT
  1125. && JUMP_P (last)
  1126. && any_condjump_p (last)
  1127. && !find_reg_note (last, REG_BR_PROB, 0))
  1128. add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
  1129. emit_jump (done_label);
  1130. }
  1131. else
  1132. {
  1133. delete_insns_since (last);
  1134. icode = CODE_FOR_nothing;
  1135. }
  1136. }
  1137. if (icode == CODE_FOR_nothing)
  1138. {
  1139. struct separate_ops ops;
  1140. int prec = GET_MODE_PRECISION (mode);
  1141. machine_mode hmode = mode_for_size (prec / 2, MODE_INT, 1);
  1142. ops.op0 = make_tree (type, op0);
  1143. ops.op1 = make_tree (type, op1);
  1144. ops.op2 = NULL_TREE;
  1145. ops.location = loc;
  1146. if (GET_MODE_2XWIDER_MODE (mode) != VOIDmode
  1147. && targetm.scalar_mode_supported_p (GET_MODE_2XWIDER_MODE (mode)))
  1148. {
  1149. machine_mode wmode = GET_MODE_2XWIDER_MODE (mode);
  1150. ops.code = WIDEN_MULT_EXPR;
  1151. ops.type
  1152. = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode), uns);
  1153. res = expand_expr_real_2 (&ops, NULL_RTX, wmode, EXPAND_NORMAL);
  1154. rtx hipart = expand_shift (RSHIFT_EXPR, wmode, res, prec,
  1155. NULL_RTX, uns);
  1156. hipart = gen_lowpart (mode, hipart);
  1157. res = gen_lowpart (mode, res);
  1158. if (uns)
  1159. /* For the unsigned multiplication, there was overflow if
  1160. HIPART is non-zero. */
  1161. do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
  1162. NULL_RTX, NULL_RTX, done_label,
  1163. PROB_VERY_LIKELY);
  1164. else
  1165. {
  1166. rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
  1167. NULL_RTX, 0);
  1168. /* RES is low half of the double width result, HIPART
  1169. the high half. There was overflow if
  1170. HIPART is different from RES < 0 ? -1 : 0. */
  1171. do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
  1172. NULL_RTX, NULL_RTX, done_label,
  1173. PROB_VERY_LIKELY);
  1174. }
  1175. }
  1176. else if (hmode != BLKmode && 2 * GET_MODE_PRECISION (hmode) == prec)
  1177. {
  1178. rtx_code_label *large_op0 = gen_label_rtx ();
  1179. rtx_code_label *small_op0_large_op1 = gen_label_rtx ();
  1180. rtx_code_label *one_small_one_large = gen_label_rtx ();
  1181. rtx_code_label *both_ops_large = gen_label_rtx ();
  1182. rtx_code_label *after_hipart_neg = uns ? NULL : gen_label_rtx ();
  1183. rtx_code_label *after_lopart_neg = uns ? NULL : gen_label_rtx ();
  1184. rtx_code_label *do_overflow = gen_label_rtx ();
  1185. rtx_code_label *hipart_different = uns ? NULL : gen_label_rtx ();
  1186. unsigned int hprec = GET_MODE_PRECISION (hmode);
  1187. rtx hipart0 = expand_shift (RSHIFT_EXPR, mode, op0, hprec,
  1188. NULL_RTX, uns);
  1189. hipart0 = gen_lowpart (hmode, hipart0);
  1190. rtx lopart0 = gen_lowpart (hmode, op0);
  1191. rtx signbit0 = const0_rtx;
  1192. if (!uns)
  1193. signbit0 = expand_shift (RSHIFT_EXPR, hmode, lopart0, hprec - 1,
  1194. NULL_RTX, 0);
  1195. rtx hipart1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec,
  1196. NULL_RTX, uns);
  1197. hipart1 = gen_lowpart (hmode, hipart1);
  1198. rtx lopart1 = gen_lowpart (hmode, op1);
  1199. rtx signbit1 = const0_rtx;
  1200. if (!uns)
  1201. signbit1 = expand_shift (RSHIFT_EXPR, hmode, lopart1, hprec - 1,
  1202. NULL_RTX, 0);
  1203. res = gen_reg_rtx (mode);
  1204. /* True if op0 resp. op1 are known to be in the range of
  1205. halfstype. */
  1206. bool op0_small_p = false;
  1207. bool op1_small_p = false;
  1208. /* True if op0 resp. op1 are known to have all zeros or all ones
  1209. in the upper half of bits, but are not known to be
  1210. op{0,1}_small_p. */
  1211. bool op0_medium_p = false;
  1212. bool op1_medium_p = false;
  1213. /* -1 if op{0,1} is known to be negative, 0 if it is known to be
  1214. nonnegative, 1 if unknown. */
  1215. int op0_sign = 1;
  1216. int op1_sign = 1;
  1217. if (pos_neg0 == 1)
  1218. op0_sign = 0;
  1219. else if (pos_neg0 == 2)
  1220. op0_sign = -1;
  1221. if (pos_neg1 == 1)
  1222. op1_sign = 0;
  1223. else if (pos_neg1 == 2)
  1224. op1_sign = -1;
  1225. unsigned int mprec0 = prec;
  1226. if (arg0 != error_mark_node)
  1227. mprec0 = get_min_precision (arg0, sign);
  1228. if (mprec0 <= hprec)
  1229. op0_small_p = true;
  1230. else if (!uns && mprec0 <= hprec + 1)
  1231. op0_medium_p = true;
  1232. unsigned int mprec1 = prec;
  1233. if (arg1 != error_mark_node)
  1234. mprec1 = get_min_precision (arg1, sign);
  1235. if (mprec1 <= hprec)
  1236. op1_small_p = true;
  1237. else if (!uns && mprec1 <= hprec + 1)
  1238. op1_medium_p = true;
  1239. int smaller_sign = 1;
  1240. int larger_sign = 1;
  1241. if (op0_small_p)
  1242. {
  1243. smaller_sign = op0_sign;
  1244. larger_sign = op1_sign;
  1245. }
  1246. else if (op1_small_p)
  1247. {
  1248. smaller_sign = op1_sign;
  1249. larger_sign = op0_sign;
  1250. }
  1251. else if (op0_sign == op1_sign)
  1252. {
  1253. smaller_sign = op0_sign;
  1254. larger_sign = op0_sign;
  1255. }
  1256. if (!op0_small_p)
  1257. do_compare_rtx_and_jump (signbit0, hipart0, NE, true, hmode,
  1258. NULL_RTX, NULL_RTX, large_op0,
  1259. PROB_UNLIKELY);
  1260. if (!op1_small_p)
  1261. do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
  1262. NULL_RTX, NULL_RTX, small_op0_large_op1,
  1263. PROB_UNLIKELY);
  1264. /* If both op0 and op1 are sign (!uns) or zero (uns) extended from
  1265. hmode to mode, the multiplication will never overflow. We can
  1266. do just one hmode x hmode => mode widening multiplication. */
  1267. rtx lopart0s = lopart0, lopart1s = lopart1;
  1268. if (GET_CODE (lopart0) == SUBREG)
  1269. {
  1270. lopart0s = shallow_copy_rtx (lopart0);
  1271. SUBREG_PROMOTED_VAR_P (lopart0s) = 1;
  1272. SUBREG_PROMOTED_SET (lopart0s, uns ? SRP_UNSIGNED : SRP_SIGNED);
  1273. }
  1274. if (GET_CODE (lopart1) == SUBREG)
  1275. {
  1276. lopart1s = shallow_copy_rtx (lopart1);
  1277. SUBREG_PROMOTED_VAR_P (lopart1s) = 1;
  1278. SUBREG_PROMOTED_SET (lopart1s, uns ? SRP_UNSIGNED : SRP_SIGNED);
  1279. }
  1280. tree halfstype = build_nonstandard_integer_type (hprec, uns);
  1281. ops.op0 = make_tree (halfstype, lopart0s);
  1282. ops.op1 = make_tree (halfstype, lopart1s);
  1283. ops.code = WIDEN_MULT_EXPR;
  1284. ops.type = type;
  1285. rtx thisres
  1286. = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1287. emit_move_insn (res, thisres);
  1288. emit_jump (done_label);
  1289. emit_label (small_op0_large_op1);
  1290. /* If op0 is sign (!uns) or zero (uns) extended from hmode to mode,
  1291. but op1 is not, just swap the arguments and handle it as op1
  1292. sign/zero extended, op0 not. */
  1293. rtx larger = gen_reg_rtx (mode);
  1294. rtx hipart = gen_reg_rtx (hmode);
  1295. rtx lopart = gen_reg_rtx (hmode);
  1296. emit_move_insn (larger, op1);
  1297. emit_move_insn (hipart, hipart1);
  1298. emit_move_insn (lopart, lopart0);
  1299. emit_jump (one_small_one_large);
  1300. emit_label (large_op0);
  1301. if (!op1_small_p)
  1302. do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
  1303. NULL_RTX, NULL_RTX, both_ops_large,
  1304. PROB_UNLIKELY);
  1305. /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode,
  1306. but op0 is not, prepare larger, hipart and lopart pseudos and
  1307. handle it together with small_op0_large_op1. */
  1308. emit_move_insn (larger, op0);
  1309. emit_move_insn (hipart, hipart0);
  1310. emit_move_insn (lopart, lopart1);
  1311. emit_label (one_small_one_large);
  1312. /* lopart is the low part of the operand that is sign extended
  1313. to mode, larger is the the other operand, hipart is the
  1314. high part of larger and lopart0 and lopart1 are the low parts
  1315. of both operands.
  1316. We perform lopart0 * lopart1 and lopart * hipart widening
  1317. multiplications. */
  1318. tree halfutype = build_nonstandard_integer_type (hprec, 1);
  1319. ops.op0 = make_tree (halfutype, lopart0);
  1320. ops.op1 = make_tree (halfutype, lopart1);
  1321. rtx lo0xlo1
  1322. = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1323. ops.op0 = make_tree (halfutype, lopart);
  1324. ops.op1 = make_tree (halfutype, hipart);
  1325. rtx loxhi = gen_reg_rtx (mode);
  1326. rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1327. emit_move_insn (loxhi, tem);
  1328. if (!uns)
  1329. {
  1330. /* if (hipart < 0) loxhi -= lopart << (bitsize / 2); */
  1331. if (larger_sign == 0)
  1332. emit_jump (after_hipart_neg);
  1333. else if (larger_sign != -1)
  1334. do_compare_rtx_and_jump (hipart, const0_rtx, GE, false, hmode,
  1335. NULL_RTX, NULL_RTX, after_hipart_neg,
  1336. PROB_EVEN);
  1337. tem = convert_modes (mode, hmode, lopart, 1);
  1338. tem = expand_shift (LSHIFT_EXPR, mode, tem, hprec, NULL_RTX, 1);
  1339. tem = expand_simple_binop (mode, MINUS, loxhi, tem, NULL_RTX,
  1340. 1, OPTAB_DIRECT);
  1341. emit_move_insn (loxhi, tem);
  1342. emit_label (after_hipart_neg);
  1343. /* if (lopart < 0) loxhi -= larger; */
  1344. if (smaller_sign == 0)
  1345. emit_jump (after_lopart_neg);
  1346. else if (smaller_sign != -1)
  1347. do_compare_rtx_and_jump (lopart, const0_rtx, GE, false, hmode,
  1348. NULL_RTX, NULL_RTX, after_lopart_neg,
  1349. PROB_EVEN);
  1350. tem = expand_simple_binop (mode, MINUS, loxhi, larger, NULL_RTX,
  1351. 1, OPTAB_DIRECT);
  1352. emit_move_insn (loxhi, tem);
  1353. emit_label (after_lopart_neg);
  1354. }
  1355. /* loxhi += (uns) lo0xlo1 >> (bitsize / 2); */
  1356. tem = expand_shift (RSHIFT_EXPR, mode, lo0xlo1, hprec, NULL_RTX, 1);
  1357. tem = expand_simple_binop (mode, PLUS, loxhi, tem, NULL_RTX,
  1358. 1, OPTAB_DIRECT);
  1359. emit_move_insn (loxhi, tem);
  1360. /* if (loxhi >> (bitsize / 2)
  1361. == (hmode) loxhi >> (bitsize / 2 - 1)) (if !uns)
  1362. if (loxhi >> (bitsize / 2) == 0 (if uns). */
  1363. rtx hipartloxhi = expand_shift (RSHIFT_EXPR, mode, loxhi, hprec,
  1364. NULL_RTX, 0);
  1365. hipartloxhi = gen_lowpart (hmode, hipartloxhi);
  1366. rtx signbitloxhi = const0_rtx;
  1367. if (!uns)
  1368. signbitloxhi = expand_shift (RSHIFT_EXPR, hmode,
  1369. gen_lowpart (hmode, loxhi),
  1370. hprec - 1, NULL_RTX, 0);
  1371. do_compare_rtx_and_jump (signbitloxhi, hipartloxhi, NE, true, hmode,
  1372. NULL_RTX, NULL_RTX, do_overflow,
  1373. PROB_VERY_UNLIKELY);
  1374. /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1; */
  1375. rtx loxhishifted = expand_shift (LSHIFT_EXPR, mode, loxhi, hprec,
  1376. NULL_RTX, 1);
  1377. tem = convert_modes (mode, hmode, gen_lowpart (hmode, lo0xlo1), 1);
  1378. tem = expand_simple_binop (mode, IOR, loxhishifted, tem, res,
  1379. 1, OPTAB_DIRECT);
  1380. if (tem != res)
  1381. emit_move_insn (res, tem);
  1382. emit_jump (done_label);
  1383. emit_label (both_ops_large);
  1384. /* If both operands are large (not sign (!uns) or zero (uns)
  1385. extended from hmode), then perform the full multiplication
  1386. which will be the result of the operation.
  1387. The only cases which don't overflow are for signed multiplication
  1388. some cases where both hipart0 and highpart1 are 0 or -1.
  1389. For unsigned multiplication when high parts are both non-zero
  1390. this overflows always. */
  1391. ops.code = MULT_EXPR;
  1392. ops.op0 = make_tree (type, op0);
  1393. ops.op1 = make_tree (type, op1);
  1394. tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1395. emit_move_insn (res, tem);
  1396. if (!uns)
  1397. {
  1398. if (!op0_medium_p)
  1399. {
  1400. tem = expand_simple_binop (hmode, PLUS, hipart0, const1_rtx,
  1401. NULL_RTX, 1, OPTAB_DIRECT);
  1402. do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
  1403. NULL_RTX, NULL_RTX, do_error,
  1404. PROB_VERY_UNLIKELY);
  1405. }
  1406. if (!op1_medium_p)
  1407. {
  1408. tem = expand_simple_binop (hmode, PLUS, hipart1, const1_rtx,
  1409. NULL_RTX, 1, OPTAB_DIRECT);
  1410. do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
  1411. NULL_RTX, NULL_RTX, do_error,
  1412. PROB_VERY_UNLIKELY);
  1413. }
  1414. /* At this point hipart{0,1} are both in [-1, 0]. If they are
  1415. the same, overflow happened if res is negative, if they are
  1416. different, overflow happened if res is positive. */
  1417. if (op0_sign != 1 && op1_sign != 1 && op0_sign != op1_sign)
  1418. emit_jump (hipart_different);
  1419. else if (op0_sign == 1 || op1_sign == 1)
  1420. do_compare_rtx_and_jump (hipart0, hipart1, NE, true, hmode,
  1421. NULL_RTX, NULL_RTX, hipart_different,
  1422. PROB_EVEN);
  1423. do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode,
  1424. NULL_RTX, NULL_RTX, do_error,
  1425. PROB_VERY_UNLIKELY);
  1426. emit_jump (done_label);
  1427. emit_label (hipart_different);
  1428. do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode,
  1429. NULL_RTX, NULL_RTX, do_error,
  1430. PROB_VERY_UNLIKELY);
  1431. emit_jump (done_label);
  1432. }
  1433. emit_label (do_overflow);
  1434. /* Overflow, do full multiplication and fallthru into do_error. */
  1435. ops.op0 = make_tree (type, op0);
  1436. ops.op1 = make_tree (type, op1);
  1437. tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1438. emit_move_insn (res, tem);
  1439. }
  1440. else
  1441. {
  1442. gcc_assert (!is_ubsan);
  1443. ops.code = MULT_EXPR;
  1444. ops.type = type;
  1445. res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1446. emit_jump (done_label);
  1447. }
  1448. }
  1449. do_error_label:
  1450. emit_label (do_error);
  1451. if (is_ubsan)
  1452. {
  1453. /* Expand the ubsan builtin call. */
  1454. push_temp_slots ();
  1455. fn = ubsan_build_overflow_builtin (MULT_EXPR, loc, TREE_TYPE (arg0),
  1456. arg0, arg1);
  1457. expand_normal (fn);
  1458. pop_temp_slots ();
  1459. do_pending_stack_adjust ();
  1460. }
  1461. else if (lhs)
  1462. write_complex_part (target, const1_rtx, true);
  1463. /* We're done. */
  1464. emit_label (done_label);
  1465. /* u1 * u2 -> sr */
  1466. if (uns0_p && uns1_p && !unsr_p)
  1467. {
  1468. rtx_code_label *all_done_label = gen_label_rtx ();
  1469. do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
  1470. NULL_RTX, all_done_label, PROB_VERY_LIKELY);
  1471. write_complex_part (target, const1_rtx, true);
  1472. emit_label (all_done_label);
  1473. }
  1474. /* s1 * u2 -> sr */
  1475. if (!uns0_p && uns1_p && !unsr_p && pos_neg1 == 3)
  1476. {
  1477. rtx_code_label *all_done_label = gen_label_rtx ();
  1478. rtx_code_label *set_noovf = gen_label_rtx ();
  1479. do_compare_rtx_and_jump (op1, const0_rtx, GE, false, mode, NULL_RTX,
  1480. NULL_RTX, all_done_label, PROB_VERY_LIKELY);
  1481. write_complex_part (target, const1_rtx, true);
  1482. do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
  1483. NULL_RTX, set_noovf, PROB_VERY_LIKELY);
  1484. do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
  1485. NULL_RTX, all_done_label, PROB_VERY_UNLIKELY);
  1486. do_compare_rtx_and_jump (op1, res, NE, true, mode, NULL_RTX, NULL_RTX,
  1487. all_done_label, PROB_VERY_UNLIKELY);
  1488. emit_label (set_noovf);
  1489. write_complex_part (target, const0_rtx, true);
  1490. emit_label (all_done_label);
  1491. }
  1492. if (lhs)
  1493. {
  1494. if (is_ubsan)
  1495. expand_ubsan_result_store (target, res);
  1496. else
  1497. expand_arith_overflow_result_store (lhs, target, mode, res);
  1498. }
  1499. }
  1500. /* Expand UBSAN_CHECK_ADD call STMT. */
  1501. static void
  1502. expand_UBSAN_CHECK_ADD (gcall *stmt)
  1503. {
  1504. location_t loc = gimple_location (stmt);
  1505. tree lhs = gimple_call_lhs (stmt);
  1506. tree arg0 = gimple_call_arg (stmt, 0);
  1507. tree arg1 = gimple_call_arg (stmt, 1);
  1508. expand_addsub_overflow (loc, PLUS_EXPR, lhs, arg0, arg1,
  1509. false, false, false, true);
  1510. }
  1511. /* Expand UBSAN_CHECK_SUB call STMT. */
  1512. static void
  1513. expand_UBSAN_CHECK_SUB (gcall *stmt)
  1514. {
  1515. location_t loc = gimple_location (stmt);
  1516. tree lhs = gimple_call_lhs (stmt);
  1517. tree arg0 = gimple_call_arg (stmt, 0);
  1518. tree arg1 = gimple_call_arg (stmt, 1);
  1519. if (integer_zerop (arg0))
  1520. expand_neg_overflow (loc, lhs, arg1, true);
  1521. else
  1522. expand_addsub_overflow (loc, MINUS_EXPR, lhs, arg0, arg1,
  1523. false, false, false, true);
  1524. }
  1525. /* Expand UBSAN_CHECK_MUL call STMT. */
  1526. static void
  1527. expand_UBSAN_CHECK_MUL (gcall *stmt)
  1528. {
  1529. location_t loc = gimple_location (stmt);
  1530. tree lhs = gimple_call_lhs (stmt);
  1531. tree arg0 = gimple_call_arg (stmt, 0);
  1532. tree arg1 = gimple_call_arg (stmt, 1);
  1533. expand_mul_overflow (loc, lhs, arg0, arg1, false, false, false, true);
  1534. }
  1535. /* Helper function for {ADD,SUB,MUL}_OVERFLOW call stmt expansion. */
  1536. static void
  1537. expand_arith_overflow (enum tree_code code, gimple stmt)
  1538. {
  1539. tree lhs = gimple_call_lhs (stmt);
  1540. if (lhs == NULL_TREE)
  1541. return;
  1542. tree arg0 = gimple_call_arg (stmt, 0);
  1543. tree arg1 = gimple_call_arg (stmt, 1);
  1544. tree type = TREE_TYPE (TREE_TYPE (lhs));
  1545. int uns0_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
  1546. int uns1_p = TYPE_UNSIGNED (TREE_TYPE (arg1));
  1547. int unsr_p = TYPE_UNSIGNED (type);
  1548. int prec0 = TYPE_PRECISION (TREE_TYPE (arg0));
  1549. int prec1 = TYPE_PRECISION (TREE_TYPE (arg1));
  1550. int precres = TYPE_PRECISION (type);
  1551. location_t loc = gimple_location (stmt);
  1552. if (!uns0_p && get_range_pos_neg (arg0) == 1)
  1553. uns0_p = true;
  1554. if (!uns1_p && get_range_pos_neg (arg1) == 1)
  1555. uns1_p = true;
  1556. int pr = get_min_precision (arg0, uns0_p ? UNSIGNED : SIGNED);
  1557. prec0 = MIN (prec0, pr);
  1558. pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED);
  1559. prec1 = MIN (prec1, pr);
  1560. /* If uns0_p && uns1_p, precop is minimum needed precision
  1561. of unsigned type to hold the exact result, otherwise
  1562. precop is minimum needed precision of signed type to
  1563. hold the exact result. */
  1564. int precop;
  1565. if (code == MULT_EXPR)
  1566. precop = prec0 + prec1 + (uns0_p != uns1_p);
  1567. else
  1568. {
  1569. if (uns0_p == uns1_p)
  1570. precop = MAX (prec0, prec1) + 1;
  1571. else if (uns0_p)
  1572. precop = MAX (prec0 + 1, prec1) + 1;
  1573. else
  1574. precop = MAX (prec0, prec1 + 1) + 1;
  1575. }
  1576. int orig_precres = precres;
  1577. do
  1578. {
  1579. if ((uns0_p && uns1_p)
  1580. ? ((precop + !unsr_p) <= precres
  1581. /* u1 - u2 -> ur can overflow, no matter what precision
  1582. the result has. */
  1583. && (code != MINUS_EXPR || !unsr_p))
  1584. : (!unsr_p && precop <= precres))
  1585. {
  1586. /* The infinity precision result will always fit into result. */
  1587. rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  1588. write_complex_part (target, const0_rtx, true);
  1589. enum machine_mode mode = TYPE_MODE (type);
  1590. struct separate_ops ops;
  1591. ops.code = code;
  1592. ops.type = type;
  1593. ops.op0 = fold_convert_loc (loc, type, arg0);
  1594. ops.op1 = fold_convert_loc (loc, type, arg1);
  1595. ops.op2 = NULL_TREE;
  1596. ops.location = loc;
  1597. rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
  1598. expand_arith_overflow_result_store (lhs, target, mode, tem);
  1599. return;
  1600. }
  1601. #ifdef WORD_REGISTER_OPERATIONS
  1602. /* For sub-word operations, if target doesn't have them, start
  1603. with precres widening right away, otherwise do it only
  1604. if the most simple cases can't be used. */
  1605. if (orig_precres == precres && precres < BITS_PER_WORD)
  1606. ;
  1607. else
  1608. #endif
  1609. if ((uns0_p && uns1_p && unsr_p && prec0 <= precres && prec1 <= precres)
  1610. || ((!uns0_p || !uns1_p) && !unsr_p
  1611. && prec0 + uns0_p <= precres
  1612. && prec1 + uns1_p <= precres))
  1613. {
  1614. arg0 = fold_convert_loc (loc, type, arg0);
  1615. arg1 = fold_convert_loc (loc, type, arg1);
  1616. switch (code)
  1617. {
  1618. case MINUS_EXPR:
  1619. if (integer_zerop (arg0) && !unsr_p)
  1620. expand_neg_overflow (loc, lhs, arg1, false);
  1621. /* FALLTHRU */
  1622. case PLUS_EXPR:
  1623. expand_addsub_overflow (loc, code, lhs, arg0, arg1,
  1624. unsr_p, unsr_p, unsr_p, false);
  1625. return;
  1626. case MULT_EXPR:
  1627. expand_mul_overflow (loc, lhs, arg0, arg1,
  1628. unsr_p, unsr_p, unsr_p, false);
  1629. return;
  1630. default:
  1631. gcc_unreachable ();
  1632. }
  1633. }
  1634. /* For sub-word operations, retry with a wider type first. */
  1635. if (orig_precres == precres && precop <= BITS_PER_WORD)
  1636. {
  1637. #ifdef WORD_REGISTER_OPERATIONS
  1638. int p = BITS_PER_WORD;
  1639. #else
  1640. int p = precop;
  1641. #endif
  1642. enum machine_mode m = smallest_mode_for_size (p, MODE_INT);
  1643. tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
  1644. uns0_p && uns1_p
  1645. && unsr_p);
  1646. p = TYPE_PRECISION (optype);
  1647. if (p > precres)
  1648. {
  1649. precres = p;
  1650. unsr_p = TYPE_UNSIGNED (optype);
  1651. type = optype;
  1652. continue;
  1653. }
  1654. }
  1655. if (prec0 <= precres && prec1 <= precres)
  1656. {
  1657. tree types[2];
  1658. if (unsr_p)
  1659. {
  1660. types[0] = build_nonstandard_integer_type (precres, 0);
  1661. types[1] = type;
  1662. }
  1663. else
  1664. {
  1665. types[0] = type;
  1666. types[1] = build_nonstandard_integer_type (precres, 1);
  1667. }
  1668. arg0 = fold_convert_loc (loc, types[uns0_p], arg0);
  1669. arg1 = fold_convert_loc (loc, types[uns1_p], arg1);
  1670. if (code != MULT_EXPR)
  1671. expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
  1672. uns0_p, uns1_p, false);
  1673. else
  1674. expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
  1675. uns0_p, uns1_p, false);
  1676. return;
  1677. }
  1678. /* Retry with a wider type. */
  1679. if (orig_precres == precres)
  1680. {
  1681. int p = MAX (prec0, prec1);
  1682. enum machine_mode m = smallest_mode_for_size (p, MODE_INT);
  1683. tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
  1684. uns0_p && uns1_p
  1685. && unsr_p);
  1686. p = TYPE_PRECISION (optype);
  1687. if (p > precres)
  1688. {
  1689. precres = p;
  1690. unsr_p = TYPE_UNSIGNED (optype);
  1691. type = optype;
  1692. continue;
  1693. }
  1694. }
  1695. gcc_unreachable ();
  1696. }
  1697. while (1);
  1698. }
  1699. /* Expand ADD_OVERFLOW STMT. */
  1700. static void
  1701. expand_ADD_OVERFLOW (gcall *stmt)
  1702. {
  1703. expand_arith_overflow (PLUS_EXPR, stmt);
  1704. }
  1705. /* Expand SUB_OVERFLOW STMT. */
  1706. static void
  1707. expand_SUB_OVERFLOW (gcall *stmt)
  1708. {
  1709. expand_arith_overflow (MINUS_EXPR, stmt);
  1710. }
  1711. /* Expand MUL_OVERFLOW STMT. */
  1712. static void
  1713. expand_MUL_OVERFLOW (gcall *stmt)
  1714. {
  1715. expand_arith_overflow (MULT_EXPR, stmt);
  1716. }
  1717. /* This should get folded in tree-vectorizer.c. */
  1718. static void
  1719. expand_LOOP_VECTORIZED (gcall *)
  1720. {
  1721. gcc_unreachable ();
  1722. }
  1723. static void
  1724. expand_MASK_LOAD (gcall *stmt)
  1725. {
  1726. struct expand_operand ops[3];
  1727. tree type, lhs, rhs, maskt;
  1728. rtx mem, target, mask;
  1729. maskt = gimple_call_arg (stmt, 2);
  1730. lhs = gimple_call_lhs (stmt);
  1731. if (lhs == NULL_TREE)
  1732. return;
  1733. type = TREE_TYPE (lhs);
  1734. rhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
  1735. gimple_call_arg (stmt, 1));
  1736. mem = expand_expr (rhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  1737. gcc_assert (MEM_P (mem));
  1738. mask = expand_normal (maskt);
  1739. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  1740. create_output_operand (&ops[0], target, TYPE_MODE (type));
  1741. create_fixed_operand (&ops[1], mem);
  1742. create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
  1743. expand_insn (optab_handler (maskload_optab, TYPE_MODE (type)), 3, ops);
  1744. }
  1745. static void
  1746. expand_MASK_STORE (gcall *stmt)
  1747. {
  1748. struct expand_operand ops[3];
  1749. tree type, lhs, rhs, maskt;
  1750. rtx mem, reg, mask;
  1751. maskt = gimple_call_arg (stmt, 2);
  1752. rhs = gimple_call_arg (stmt, 3);
  1753. type = TREE_TYPE (rhs);
  1754. lhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
  1755. gimple_call_arg (stmt, 1));
  1756. mem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  1757. gcc_assert (MEM_P (mem));
  1758. mask = expand_normal (maskt);
  1759. reg = expand_normal (rhs);
  1760. create_fixed_operand (&ops[0], mem);
  1761. create_input_operand (&ops[1], reg, TYPE_MODE (type));
  1762. create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
  1763. expand_insn (optab_handler (maskstore_optab, TYPE_MODE (type)), 3, ops);
  1764. }
  1765. static void
  1766. expand_ABNORMAL_DISPATCHER (gcall *)
  1767. {
  1768. }
  1769. static void
  1770. expand_BUILTIN_EXPECT (gcall *stmt)
  1771. {
  1772. /* When guessing was done, the hints should be already stripped away. */
  1773. gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ());
  1774. rtx target;
  1775. tree lhs = gimple_call_lhs (stmt);
  1776. if (lhs)
  1777. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  1778. else
  1779. target = const0_rtx;
  1780. rtx val = expand_expr (gimple_call_arg (stmt, 0), target, VOIDmode, EXPAND_NORMAL);
  1781. if (lhs && val != target)
  1782. emit_move_insn (target, val);
  1783. }
  1784. /* Routines to expand each internal function, indexed by function number.
  1785. Each routine has the prototype:
  1786. expand_<NAME> (gcall *stmt)
  1787. where STMT is the statement that performs the call. */
  1788. static void (*const internal_fn_expanders[]) (gcall *) = {
  1789. #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) expand_##CODE,
  1790. #include "internal-fn.def"
  1791. #undef DEF_INTERNAL_FN
  1792. 0
  1793. };
  1794. /* Expand STMT, which is a call to internal function FN. */
  1795. void
  1796. expand_internal_call (gcall *stmt)
  1797. {
  1798. internal_fn_expanders[(int) gimple_call_internal_fn (stmt)] (stmt);
  1799. }