tree-chrec.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /* Chains of recurrences.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <pop@cri.ensmp.fr>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This file implements operations on chains of recurrences. Chains
  17. of recurrences are used for modeling evolution functions of scalar
  18. variables.
  19. */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "hash-set.h"
  24. #include "machmode.h"
  25. #include "vec.h"
  26. #include "double-int.h"
  27. #include "input.h"
  28. #include "alias.h"
  29. #include "symtab.h"
  30. #include "options.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "real.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "tree-pretty-print.h"
  37. #include "cfgloop.h"
  38. #include "predict.h"
  39. #include "tm.h"
  40. #include "hard-reg-set.h"
  41. #include "input.h"
  42. #include "function.h"
  43. #include "dominance.h"
  44. #include "cfg.h"
  45. #include "basic-block.h"
  46. #include "gimple-expr.h"
  47. #include "tree-ssa-loop-ivopts.h"
  48. #include "tree-ssa-loop-niter.h"
  49. #include "tree-chrec.h"
  50. #include "dumpfile.h"
  51. #include "params.h"
  52. #include "tree-scalar-evolution.h"
  53. /* Extended folder for chrecs. */
  54. /* Determines whether CST is not a constant evolution. */
  55. static inline bool
  56. is_not_constant_evolution (const_tree cst)
  57. {
  58. return (TREE_CODE (cst) == POLYNOMIAL_CHREC);
  59. }
  60. /* Fold CODE for a polynomial function and a constant. */
  61. static inline tree
  62. chrec_fold_poly_cst (enum tree_code code,
  63. tree type,
  64. tree poly,
  65. tree cst)
  66. {
  67. gcc_assert (poly);
  68. gcc_assert (cst);
  69. gcc_assert (TREE_CODE (poly) == POLYNOMIAL_CHREC);
  70. gcc_checking_assert (!is_not_constant_evolution (cst));
  71. gcc_checking_assert (useless_type_conversion_p (type, chrec_type (poly)));
  72. switch (code)
  73. {
  74. case PLUS_EXPR:
  75. return build_polynomial_chrec
  76. (CHREC_VARIABLE (poly),
  77. chrec_fold_plus (type, CHREC_LEFT (poly), cst),
  78. CHREC_RIGHT (poly));
  79. case MINUS_EXPR:
  80. return build_polynomial_chrec
  81. (CHREC_VARIABLE (poly),
  82. chrec_fold_minus (type, CHREC_LEFT (poly), cst),
  83. CHREC_RIGHT (poly));
  84. case MULT_EXPR:
  85. return build_polynomial_chrec
  86. (CHREC_VARIABLE (poly),
  87. chrec_fold_multiply (type, CHREC_LEFT (poly), cst),
  88. chrec_fold_multiply (type, CHREC_RIGHT (poly), cst));
  89. default:
  90. return chrec_dont_know;
  91. }
  92. }
  93. /* Fold the addition of two polynomial functions. */
  94. static inline tree
  95. chrec_fold_plus_poly_poly (enum tree_code code,
  96. tree type,
  97. tree poly0,
  98. tree poly1)
  99. {
  100. tree left, right;
  101. struct loop *loop0 = get_chrec_loop (poly0);
  102. struct loop *loop1 = get_chrec_loop (poly1);
  103. tree rtype = code == POINTER_PLUS_EXPR ? chrec_type (poly1) : type;
  104. gcc_assert (poly0);
  105. gcc_assert (poly1);
  106. gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC);
  107. gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC);
  108. if (POINTER_TYPE_P (chrec_type (poly0)))
  109. gcc_checking_assert (ptrofftype_p (chrec_type (poly1))
  110. && useless_type_conversion_p (type, chrec_type (poly0)));
  111. else
  112. gcc_checking_assert (useless_type_conversion_p (type, chrec_type (poly0))
  113. && useless_type_conversion_p (type, chrec_type (poly1)));
  114. /*
  115. {a, +, b}_1 + {c, +, d}_2 -> {{a, +, b}_1 + c, +, d}_2,
  116. {a, +, b}_2 + {c, +, d}_1 -> {{c, +, d}_1 + a, +, b}_2,
  117. {a, +, b}_x + {c, +, d}_x -> {a+c, +, b+d}_x. */
  118. if (flow_loop_nested_p (loop0, loop1))
  119. {
  120. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  121. return build_polynomial_chrec
  122. (CHREC_VARIABLE (poly1),
  123. chrec_fold_plus (type, poly0, CHREC_LEFT (poly1)),
  124. CHREC_RIGHT (poly1));
  125. else
  126. return build_polynomial_chrec
  127. (CHREC_VARIABLE (poly1),
  128. chrec_fold_minus (type, poly0, CHREC_LEFT (poly1)),
  129. chrec_fold_multiply (type, CHREC_RIGHT (poly1),
  130. SCALAR_FLOAT_TYPE_P (type)
  131. ? build_real (type, dconstm1)
  132. : build_int_cst_type (type, -1)));
  133. }
  134. if (flow_loop_nested_p (loop1, loop0))
  135. {
  136. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  137. return build_polynomial_chrec
  138. (CHREC_VARIABLE (poly0),
  139. chrec_fold_plus (type, CHREC_LEFT (poly0), poly1),
  140. CHREC_RIGHT (poly0));
  141. else
  142. return build_polynomial_chrec
  143. (CHREC_VARIABLE (poly0),
  144. chrec_fold_minus (type, CHREC_LEFT (poly0), poly1),
  145. CHREC_RIGHT (poly0));
  146. }
  147. /* This function should never be called for chrecs of loops that
  148. do not belong to the same loop nest. */
  149. gcc_assert (loop0 == loop1);
  150. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  151. {
  152. left = chrec_fold_plus
  153. (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
  154. right = chrec_fold_plus
  155. (rtype, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
  156. }
  157. else
  158. {
  159. left = chrec_fold_minus
  160. (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
  161. right = chrec_fold_minus
  162. (type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
  163. }
  164. if (chrec_zerop (right))
  165. return left;
  166. else
  167. return build_polynomial_chrec
  168. (CHREC_VARIABLE (poly0), left, right);
  169. }
  170. /* Fold the multiplication of two polynomial functions. */
  171. static inline tree
  172. chrec_fold_multiply_poly_poly (tree type,
  173. tree poly0,
  174. tree poly1)
  175. {
  176. tree t0, t1, t2;
  177. int var;
  178. struct loop *loop0 = get_chrec_loop (poly0);
  179. struct loop *loop1 = get_chrec_loop (poly1);
  180. gcc_assert (poly0);
  181. gcc_assert (poly1);
  182. gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC);
  183. gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC);
  184. gcc_checking_assert (useless_type_conversion_p (type, chrec_type (poly0))
  185. && useless_type_conversion_p (type, chrec_type (poly1)));
  186. /* {a, +, b}_1 * {c, +, d}_2 -> {c*{a, +, b}_1, +, d}_2,
  187. {a, +, b}_2 * {c, +, d}_1 -> {a*{c, +, d}_1, +, b}_2,
  188. {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
  189. if (flow_loop_nested_p (loop0, loop1))
  190. /* poly0 is a constant wrt. poly1. */
  191. return build_polynomial_chrec
  192. (CHREC_VARIABLE (poly1),
  193. chrec_fold_multiply (type, CHREC_LEFT (poly1), poly0),
  194. CHREC_RIGHT (poly1));
  195. if (flow_loop_nested_p (loop1, loop0))
  196. /* poly1 is a constant wrt. poly0. */
  197. return build_polynomial_chrec
  198. (CHREC_VARIABLE (poly0),
  199. chrec_fold_multiply (type, CHREC_LEFT (poly0), poly1),
  200. CHREC_RIGHT (poly0));
  201. gcc_assert (loop0 == loop1);
  202. /* poly0 and poly1 are two polynomials in the same variable,
  203. {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
  204. /* "a*c". */
  205. t0 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
  206. /* "a*d + b*c". */
  207. t1 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_RIGHT (poly1));
  208. t1 = chrec_fold_plus (type, t1, chrec_fold_multiply (type,
  209. CHREC_RIGHT (poly0),
  210. CHREC_LEFT (poly1)));
  211. /* "b*d". */
  212. t2 = chrec_fold_multiply (type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
  213. /* "a*d + b*c + b*d". */
  214. t1 = chrec_fold_plus (type, t1, t2);
  215. /* "2*b*d". */
  216. t2 = chrec_fold_multiply (type, SCALAR_FLOAT_TYPE_P (type)
  217. ? build_real (type, dconst2)
  218. : build_int_cst (type, 2), t2);
  219. var = CHREC_VARIABLE (poly0);
  220. return build_polynomial_chrec (var, t0,
  221. build_polynomial_chrec (var, t1, t2));
  222. }
  223. /* When the operands are automatically_generated_chrec_p, the fold has
  224. to respect the semantics of the operands. */
  225. static inline tree
  226. chrec_fold_automatically_generated_operands (tree op0,
  227. tree op1)
  228. {
  229. if (op0 == chrec_dont_know
  230. || op1 == chrec_dont_know)
  231. return chrec_dont_know;
  232. if (op0 == chrec_known
  233. || op1 == chrec_known)
  234. return chrec_known;
  235. if (op0 == chrec_not_analyzed_yet
  236. || op1 == chrec_not_analyzed_yet)
  237. return chrec_not_analyzed_yet;
  238. /* The default case produces a safe result. */
  239. return chrec_dont_know;
  240. }
  241. /* Fold the addition of two chrecs. */
  242. static tree
  243. chrec_fold_plus_1 (enum tree_code code, tree type,
  244. tree op0, tree op1)
  245. {
  246. if (automatically_generated_chrec_p (op0)
  247. || automatically_generated_chrec_p (op1))
  248. return chrec_fold_automatically_generated_operands (op0, op1);
  249. switch (TREE_CODE (op0))
  250. {
  251. case POLYNOMIAL_CHREC:
  252. gcc_checking_assert
  253. (!chrec_contains_symbols_defined_in_loop (op0, CHREC_VARIABLE (op0)));
  254. switch (TREE_CODE (op1))
  255. {
  256. case POLYNOMIAL_CHREC:
  257. gcc_checking_assert
  258. (!chrec_contains_symbols_defined_in_loop (op1,
  259. CHREC_VARIABLE (op1)));
  260. return chrec_fold_plus_poly_poly (code, type, op0, op1);
  261. CASE_CONVERT:
  262. if (tree_contains_chrecs (op1, NULL))
  263. return chrec_dont_know;
  264. default:
  265. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  266. return build_polynomial_chrec
  267. (CHREC_VARIABLE (op0),
  268. chrec_fold_plus (type, CHREC_LEFT (op0), op1),
  269. CHREC_RIGHT (op0));
  270. else
  271. return build_polynomial_chrec
  272. (CHREC_VARIABLE (op0),
  273. chrec_fold_minus (type, CHREC_LEFT (op0), op1),
  274. CHREC_RIGHT (op0));
  275. }
  276. CASE_CONVERT:
  277. if (tree_contains_chrecs (op0, NULL))
  278. return chrec_dont_know;
  279. default:
  280. switch (TREE_CODE (op1))
  281. {
  282. case POLYNOMIAL_CHREC:
  283. gcc_checking_assert
  284. (!chrec_contains_symbols_defined_in_loop (op1,
  285. CHREC_VARIABLE (op1)));
  286. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  287. return build_polynomial_chrec
  288. (CHREC_VARIABLE (op1),
  289. chrec_fold_plus (type, op0, CHREC_LEFT (op1)),
  290. CHREC_RIGHT (op1));
  291. else
  292. return build_polynomial_chrec
  293. (CHREC_VARIABLE (op1),
  294. chrec_fold_minus (type, op0, CHREC_LEFT (op1)),
  295. chrec_fold_multiply (type, CHREC_RIGHT (op1),
  296. SCALAR_FLOAT_TYPE_P (type)
  297. ? build_real (type, dconstm1)
  298. : build_int_cst_type (type, -1)));
  299. CASE_CONVERT:
  300. if (tree_contains_chrecs (op1, NULL))
  301. return chrec_dont_know;
  302. default:
  303. {
  304. int size = 0;
  305. if ((tree_contains_chrecs (op0, &size)
  306. || tree_contains_chrecs (op1, &size))
  307. && size < PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
  308. return build2 (code, type, op0, op1);
  309. else if (size < PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
  310. {
  311. if (code == POINTER_PLUS_EXPR)
  312. return fold_build_pointer_plus (fold_convert (type, op0),
  313. op1);
  314. else
  315. return fold_build2 (code, type,
  316. fold_convert (type, op0),
  317. fold_convert (type, op1));
  318. }
  319. else
  320. return chrec_dont_know;
  321. }
  322. }
  323. }
  324. }
  325. /* Fold the addition of two chrecs. */
  326. tree
  327. chrec_fold_plus (tree type,
  328. tree op0,
  329. tree op1)
  330. {
  331. enum tree_code code;
  332. if (automatically_generated_chrec_p (op0)
  333. || automatically_generated_chrec_p (op1))
  334. return chrec_fold_automatically_generated_operands (op0, op1);
  335. if (integer_zerop (op0))
  336. return chrec_convert (type, op1, NULL);
  337. if (integer_zerop (op1))
  338. return chrec_convert (type, op0, NULL);
  339. if (POINTER_TYPE_P (type))
  340. code = POINTER_PLUS_EXPR;
  341. else
  342. code = PLUS_EXPR;
  343. return chrec_fold_plus_1 (code, type, op0, op1);
  344. }
  345. /* Fold the subtraction of two chrecs. */
  346. tree
  347. chrec_fold_minus (tree type,
  348. tree op0,
  349. tree op1)
  350. {
  351. if (automatically_generated_chrec_p (op0)
  352. || automatically_generated_chrec_p (op1))
  353. return chrec_fold_automatically_generated_operands (op0, op1);
  354. if (integer_zerop (op1))
  355. return op0;
  356. return chrec_fold_plus_1 (MINUS_EXPR, type, op0, op1);
  357. }
  358. /* Fold the multiplication of two chrecs. */
  359. tree
  360. chrec_fold_multiply (tree type,
  361. tree op0,
  362. tree op1)
  363. {
  364. if (automatically_generated_chrec_p (op0)
  365. || automatically_generated_chrec_p (op1))
  366. return chrec_fold_automatically_generated_operands (op0, op1);
  367. switch (TREE_CODE (op0))
  368. {
  369. case POLYNOMIAL_CHREC:
  370. gcc_checking_assert
  371. (!chrec_contains_symbols_defined_in_loop (op0, CHREC_VARIABLE (op0)));
  372. switch (TREE_CODE (op1))
  373. {
  374. case POLYNOMIAL_CHREC:
  375. gcc_checking_assert
  376. (!chrec_contains_symbols_defined_in_loop (op1,
  377. CHREC_VARIABLE (op1)));
  378. return chrec_fold_multiply_poly_poly (type, op0, op1);
  379. CASE_CONVERT:
  380. if (tree_contains_chrecs (op1, NULL))
  381. return chrec_dont_know;
  382. default:
  383. if (integer_onep (op1))
  384. return op0;
  385. if (integer_zerop (op1))
  386. return build_int_cst (type, 0);
  387. return build_polynomial_chrec
  388. (CHREC_VARIABLE (op0),
  389. chrec_fold_multiply (type, CHREC_LEFT (op0), op1),
  390. chrec_fold_multiply (type, CHREC_RIGHT (op0), op1));
  391. }
  392. CASE_CONVERT:
  393. if (tree_contains_chrecs (op0, NULL))
  394. return chrec_dont_know;
  395. default:
  396. if (integer_onep (op0))
  397. return op1;
  398. if (integer_zerop (op0))
  399. return build_int_cst (type, 0);
  400. switch (TREE_CODE (op1))
  401. {
  402. case POLYNOMIAL_CHREC:
  403. gcc_checking_assert
  404. (!chrec_contains_symbols_defined_in_loop (op1,
  405. CHREC_VARIABLE (op1)));
  406. return build_polynomial_chrec
  407. (CHREC_VARIABLE (op1),
  408. chrec_fold_multiply (type, CHREC_LEFT (op1), op0),
  409. chrec_fold_multiply (type, CHREC_RIGHT (op1), op0));
  410. CASE_CONVERT:
  411. if (tree_contains_chrecs (op1, NULL))
  412. return chrec_dont_know;
  413. default:
  414. if (integer_onep (op1))
  415. return op0;
  416. if (integer_zerop (op1))
  417. return build_int_cst (type, 0);
  418. return fold_build2 (MULT_EXPR, type, op0, op1);
  419. }
  420. }
  421. }
  422. /* Operations. */
  423. /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
  424. calculation overflows, otherwise return C(n,k) with type TYPE. */
  425. static tree
  426. tree_fold_binomial (tree type, tree n, unsigned int k)
  427. {
  428. bool overflow;
  429. unsigned int i;
  430. tree res;
  431. /* Handle the most frequent cases. */
  432. if (k == 0)
  433. return build_int_cst (type, 1);
  434. if (k == 1)
  435. return fold_convert (type, n);
  436. /* Check that k <= n. */
  437. if (wi::ltu_p (n, k))
  438. return NULL_TREE;
  439. /* Denominator = 2. */
  440. wide_int denom = wi::two (TYPE_PRECISION (TREE_TYPE (n)));
  441. /* Index = Numerator-1. */
  442. wide_int idx = wi::sub (n, 1);
  443. /* Numerator = Numerator*Index = n*(n-1). */
  444. wide_int num = wi::smul (n, idx, &overflow);
  445. if (overflow)
  446. return NULL_TREE;
  447. for (i = 3; i <= k; i++)
  448. {
  449. /* Index--. */
  450. --idx;
  451. /* Numerator *= Index. */
  452. num = wi::smul (num, idx, &overflow);
  453. if (overflow)
  454. return NULL_TREE;
  455. /* Denominator *= i. */
  456. denom *= i;
  457. }
  458. /* Result = Numerator / Denominator. */
  459. wide_int di_res = wi::udiv_trunc (num, denom);
  460. res = wide_int_to_tree (type, di_res);
  461. return int_fits_type_p (res, type) ? res : NULL_TREE;
  462. }
  463. /* Helper function. Use the Newton's interpolating formula for
  464. evaluating the value of the evolution function. */
  465. static tree
  466. chrec_evaluate (unsigned var, tree chrec, tree n, unsigned int k)
  467. {
  468. tree arg0, arg1, binomial_n_k;
  469. tree type = TREE_TYPE (chrec);
  470. struct loop *var_loop = get_loop (cfun, var);
  471. while (TREE_CODE (chrec) == POLYNOMIAL_CHREC
  472. && flow_loop_nested_p (var_loop, get_chrec_loop (chrec)))
  473. chrec = CHREC_LEFT (chrec);
  474. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC
  475. && CHREC_VARIABLE (chrec) == var)
  476. {
  477. arg1 = chrec_evaluate (var, CHREC_RIGHT (chrec), n, k + 1);
  478. if (arg1 == chrec_dont_know)
  479. return chrec_dont_know;
  480. binomial_n_k = tree_fold_binomial (type, n, k);
  481. if (!binomial_n_k)
  482. return chrec_dont_know;
  483. arg0 = fold_build2 (MULT_EXPR, type,
  484. CHREC_LEFT (chrec), binomial_n_k);
  485. return chrec_fold_plus (type, arg0, arg1);
  486. }
  487. binomial_n_k = tree_fold_binomial (type, n, k);
  488. if (!binomial_n_k)
  489. return chrec_dont_know;
  490. return fold_build2 (MULT_EXPR, type, chrec, binomial_n_k);
  491. }
  492. /* Evaluates "CHREC (X)" when the varying variable is VAR.
  493. Example: Given the following parameters,
  494. var = 1
  495. chrec = {3, +, 4}_1
  496. x = 10
  497. The result is given by the Newton's interpolating formula:
  498. 3 * \binom{10}{0} + 4 * \binom{10}{1}.
  499. */
  500. tree
  501. chrec_apply (unsigned var,
  502. tree chrec,
  503. tree x)
  504. {
  505. tree type = chrec_type (chrec);
  506. tree res = chrec_dont_know;
  507. if (automatically_generated_chrec_p (chrec)
  508. || automatically_generated_chrec_p (x)
  509. /* When the symbols are defined in an outer loop, it is possible
  510. to symbolically compute the apply, since the symbols are
  511. constants with respect to the varying loop. */
  512. || chrec_contains_symbols_defined_in_loop (chrec, var))
  513. return chrec_dont_know;
  514. if (dump_file && (dump_flags & TDF_SCEV))
  515. fprintf (dump_file, "(chrec_apply \n");
  516. if (TREE_CODE (x) == INTEGER_CST && SCALAR_FLOAT_TYPE_P (type))
  517. x = build_real_from_int_cst (type, x);
  518. switch (TREE_CODE (chrec))
  519. {
  520. case POLYNOMIAL_CHREC:
  521. if (evolution_function_is_affine_p (chrec))
  522. {
  523. if (CHREC_VARIABLE (chrec) != var)
  524. return build_polynomial_chrec
  525. (CHREC_VARIABLE (chrec),
  526. chrec_apply (var, CHREC_LEFT (chrec), x),
  527. chrec_apply (var, CHREC_RIGHT (chrec), x));
  528. /* "{a, +, b} (x)" -> "a + b*x". */
  529. x = chrec_convert_rhs (type, x, NULL);
  530. res = chrec_fold_multiply (TREE_TYPE (x), CHREC_RIGHT (chrec), x);
  531. res = chrec_fold_plus (type, CHREC_LEFT (chrec), res);
  532. }
  533. else if (TREE_CODE (x) == INTEGER_CST
  534. && tree_int_cst_sgn (x) == 1)
  535. /* testsuite/.../ssa-chrec-38.c. */
  536. res = chrec_evaluate (var, chrec, x, 0);
  537. else
  538. res = chrec_dont_know;
  539. break;
  540. CASE_CONVERT:
  541. res = chrec_convert (TREE_TYPE (chrec),
  542. chrec_apply (var, TREE_OPERAND (chrec, 0), x),
  543. NULL);
  544. break;
  545. default:
  546. res = chrec;
  547. break;
  548. }
  549. if (dump_file && (dump_flags & TDF_SCEV))
  550. {
  551. fprintf (dump_file, " (varying_loop = %d\n", var);
  552. fprintf (dump_file, ")\n (chrec = ");
  553. print_generic_expr (dump_file, chrec, 0);
  554. fprintf (dump_file, ")\n (x = ");
  555. print_generic_expr (dump_file, x, 0);
  556. fprintf (dump_file, ")\n (res = ");
  557. print_generic_expr (dump_file, res, 0);
  558. fprintf (dump_file, "))\n");
  559. }
  560. return res;
  561. }
  562. /* For a given CHREC and an induction variable map IV_MAP that maps
  563. (loop->num, expr) for every loop number of the current_loops an
  564. expression, calls chrec_apply when the expression is not NULL. */
  565. tree
  566. chrec_apply_map (tree chrec, vec<tree> iv_map)
  567. {
  568. int i;
  569. tree expr;
  570. FOR_EACH_VEC_ELT (iv_map, i, expr)
  571. if (expr)
  572. chrec = chrec_apply (i, chrec, expr);
  573. return chrec;
  574. }
  575. /* Replaces the initial condition in CHREC with INIT_COND. */
  576. tree
  577. chrec_replace_initial_condition (tree chrec,
  578. tree init_cond)
  579. {
  580. if (automatically_generated_chrec_p (chrec))
  581. return chrec;
  582. gcc_assert (chrec_type (chrec) == chrec_type (init_cond));
  583. switch (TREE_CODE (chrec))
  584. {
  585. case POLYNOMIAL_CHREC:
  586. return build_polynomial_chrec
  587. (CHREC_VARIABLE (chrec),
  588. chrec_replace_initial_condition (CHREC_LEFT (chrec), init_cond),
  589. CHREC_RIGHT (chrec));
  590. default:
  591. return init_cond;
  592. }
  593. }
  594. /* Returns the initial condition of a given CHREC. */
  595. tree
  596. initial_condition (tree chrec)
  597. {
  598. if (automatically_generated_chrec_p (chrec))
  599. return chrec;
  600. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
  601. return initial_condition (CHREC_LEFT (chrec));
  602. else
  603. return chrec;
  604. }
  605. /* Returns a univariate function that represents the evolution in
  606. LOOP_NUM. Mask the evolution of any other loop. */
  607. tree
  608. hide_evolution_in_other_loops_than_loop (tree chrec,
  609. unsigned loop_num)
  610. {
  611. struct loop *loop = get_loop (cfun, loop_num), *chloop;
  612. if (automatically_generated_chrec_p (chrec))
  613. return chrec;
  614. switch (TREE_CODE (chrec))
  615. {
  616. case POLYNOMIAL_CHREC:
  617. chloop = get_chrec_loop (chrec);
  618. if (chloop == loop)
  619. return build_polynomial_chrec
  620. (loop_num,
  621. hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
  622. loop_num),
  623. CHREC_RIGHT (chrec));
  624. else if (flow_loop_nested_p (chloop, loop))
  625. /* There is no evolution in this loop. */
  626. return initial_condition (chrec);
  627. else
  628. {
  629. gcc_assert (flow_loop_nested_p (loop, chloop));
  630. return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
  631. loop_num);
  632. }
  633. default:
  634. return chrec;
  635. }
  636. }
  637. /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
  638. true, otherwise returns the initial condition in LOOP_NUM. */
  639. static tree
  640. chrec_component_in_loop_num (tree chrec,
  641. unsigned loop_num,
  642. bool right)
  643. {
  644. tree component;
  645. struct loop *loop = get_loop (cfun, loop_num), *chloop;
  646. if (automatically_generated_chrec_p (chrec))
  647. return chrec;
  648. switch (TREE_CODE (chrec))
  649. {
  650. case POLYNOMIAL_CHREC:
  651. chloop = get_chrec_loop (chrec);
  652. if (chloop == loop)
  653. {
  654. if (right)
  655. component = CHREC_RIGHT (chrec);
  656. else
  657. component = CHREC_LEFT (chrec);
  658. if (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC
  659. || CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec))
  660. return component;
  661. else
  662. return build_polynomial_chrec
  663. (loop_num,
  664. chrec_component_in_loop_num (CHREC_LEFT (chrec),
  665. loop_num,
  666. right),
  667. component);
  668. }
  669. else if (flow_loop_nested_p (chloop, loop))
  670. /* There is no evolution part in this loop. */
  671. return NULL_TREE;
  672. else
  673. {
  674. gcc_assert (flow_loop_nested_p (loop, chloop));
  675. return chrec_component_in_loop_num (CHREC_LEFT (chrec),
  676. loop_num,
  677. right);
  678. }
  679. default:
  680. if (right)
  681. return NULL_TREE;
  682. else
  683. return chrec;
  684. }
  685. }
  686. /* Returns the evolution part in LOOP_NUM. Example: the call
  687. evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
  688. {1, +, 2}_1 */
  689. tree
  690. evolution_part_in_loop_num (tree chrec,
  691. unsigned loop_num)
  692. {
  693. return chrec_component_in_loop_num (chrec, loop_num, true);
  694. }
  695. /* Returns the initial condition in LOOP_NUM. Example: the call
  696. initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
  697. {0, +, 1}_1 */
  698. tree
  699. initial_condition_in_loop_num (tree chrec,
  700. unsigned loop_num)
  701. {
  702. return chrec_component_in_loop_num (chrec, loop_num, false);
  703. }
  704. /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
  705. This function is essentially used for setting the evolution to
  706. chrec_dont_know, for example after having determined that it is
  707. impossible to say how many times a loop will execute. */
  708. tree
  709. reset_evolution_in_loop (unsigned loop_num,
  710. tree chrec,
  711. tree new_evol)
  712. {
  713. struct loop *loop = get_loop (cfun, loop_num);
  714. if (POINTER_TYPE_P (chrec_type (chrec)))
  715. gcc_assert (ptrofftype_p (chrec_type (new_evol)));
  716. else
  717. gcc_assert (chrec_type (chrec) == chrec_type (new_evol));
  718. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC
  719. && flow_loop_nested_p (loop, get_chrec_loop (chrec)))
  720. {
  721. tree left = reset_evolution_in_loop (loop_num, CHREC_LEFT (chrec),
  722. new_evol);
  723. tree right = reset_evolution_in_loop (loop_num, CHREC_RIGHT (chrec),
  724. new_evol);
  725. return build3 (POLYNOMIAL_CHREC, TREE_TYPE (left),
  726. CHREC_VAR (chrec), left, right);
  727. }
  728. while (TREE_CODE (chrec) == POLYNOMIAL_CHREC
  729. && CHREC_VARIABLE (chrec) == loop_num)
  730. chrec = CHREC_LEFT (chrec);
  731. return build_polynomial_chrec (loop_num, chrec, new_evol);
  732. }
  733. /* Merges two evolution functions that were found by following two
  734. alternate paths of a conditional expression. */
  735. tree
  736. chrec_merge (tree chrec1,
  737. tree chrec2)
  738. {
  739. if (chrec1 == chrec_dont_know
  740. || chrec2 == chrec_dont_know)
  741. return chrec_dont_know;
  742. if (chrec1 == chrec_known
  743. || chrec2 == chrec_known)
  744. return chrec_known;
  745. if (chrec1 == chrec_not_analyzed_yet)
  746. return chrec2;
  747. if (chrec2 == chrec_not_analyzed_yet)
  748. return chrec1;
  749. if (eq_evolutions_p (chrec1, chrec2))
  750. return chrec1;
  751. return chrec_dont_know;
  752. }
  753. /* Observers. */
  754. /* Helper function for is_multivariate_chrec. */
  755. static bool
  756. is_multivariate_chrec_rec (const_tree chrec, unsigned int rec_var)
  757. {
  758. if (chrec == NULL_TREE)
  759. return false;
  760. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
  761. {
  762. if (CHREC_VARIABLE (chrec) != rec_var)
  763. return true;
  764. else
  765. return (is_multivariate_chrec_rec (CHREC_LEFT (chrec), rec_var)
  766. || is_multivariate_chrec_rec (CHREC_RIGHT (chrec), rec_var));
  767. }
  768. else
  769. return false;
  770. }
  771. /* Determine whether the given chrec is multivariate or not. */
  772. bool
  773. is_multivariate_chrec (const_tree chrec)
  774. {
  775. if (chrec == NULL_TREE)
  776. return false;
  777. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
  778. return (is_multivariate_chrec_rec (CHREC_LEFT (chrec),
  779. CHREC_VARIABLE (chrec))
  780. || is_multivariate_chrec_rec (CHREC_RIGHT (chrec),
  781. CHREC_VARIABLE (chrec)));
  782. else
  783. return false;
  784. }
  785. /* Determines whether the chrec contains symbolic names or not. */
  786. bool
  787. chrec_contains_symbols (const_tree chrec)
  788. {
  789. int i, n;
  790. if (chrec == NULL_TREE)
  791. return false;
  792. if (TREE_CODE (chrec) == SSA_NAME
  793. || TREE_CODE (chrec) == VAR_DECL
  794. || TREE_CODE (chrec) == PARM_DECL
  795. || TREE_CODE (chrec) == FUNCTION_DECL
  796. || TREE_CODE (chrec) == LABEL_DECL
  797. || TREE_CODE (chrec) == RESULT_DECL
  798. || TREE_CODE (chrec) == FIELD_DECL)
  799. return true;
  800. n = TREE_OPERAND_LENGTH (chrec);
  801. for (i = 0; i < n; i++)
  802. if (chrec_contains_symbols (TREE_OPERAND (chrec, i)))
  803. return true;
  804. return false;
  805. }
  806. /* Determines whether the chrec contains undetermined coefficients. */
  807. bool
  808. chrec_contains_undetermined (const_tree chrec)
  809. {
  810. int i, n;
  811. if (chrec == chrec_dont_know)
  812. return true;
  813. if (chrec == NULL_TREE)
  814. return false;
  815. n = TREE_OPERAND_LENGTH (chrec);
  816. for (i = 0; i < n; i++)
  817. if (chrec_contains_undetermined (TREE_OPERAND (chrec, i)))
  818. return true;
  819. return false;
  820. }
  821. /* Determines whether the tree EXPR contains chrecs, and increment
  822. SIZE if it is not a NULL pointer by an estimation of the depth of
  823. the tree. */
  824. bool
  825. tree_contains_chrecs (const_tree expr, int *size)
  826. {
  827. int i, n;
  828. if (expr == NULL_TREE)
  829. return false;
  830. if (size)
  831. (*size)++;
  832. if (tree_is_chrec (expr))
  833. return true;
  834. n = TREE_OPERAND_LENGTH (expr);
  835. for (i = 0; i < n; i++)
  836. if (tree_contains_chrecs (TREE_OPERAND (expr, i), size))
  837. return true;
  838. return false;
  839. }
  840. /* Recursive helper function. */
  841. static bool
  842. evolution_function_is_invariant_rec_p (tree chrec, int loopnum)
  843. {
  844. if (evolution_function_is_constant_p (chrec))
  845. return true;
  846. if (TREE_CODE (chrec) == SSA_NAME
  847. && (loopnum == 0
  848. || expr_invariant_in_loop_p (get_loop (cfun, loopnum), chrec)))
  849. return true;
  850. if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
  851. {
  852. if (CHREC_VARIABLE (chrec) == (unsigned) loopnum
  853. || flow_loop_nested_p (get_loop (cfun, loopnum),
  854. get_chrec_loop (chrec))
  855. || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec),
  856. loopnum)
  857. || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec),
  858. loopnum))
  859. return false;
  860. return true;
  861. }
  862. switch (TREE_OPERAND_LENGTH (chrec))
  863. {
  864. case 2:
  865. if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 1),
  866. loopnum))
  867. return false;
  868. case 1:
  869. if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 0),
  870. loopnum))
  871. return false;
  872. return true;
  873. default:
  874. return false;
  875. }
  876. return false;
  877. }
  878. /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
  879. bool
  880. evolution_function_is_invariant_p (tree chrec, int loopnum)
  881. {
  882. return evolution_function_is_invariant_rec_p (chrec, loopnum);
  883. }
  884. /* Determine whether the given tree is an affine multivariate
  885. evolution. */
  886. bool
  887. evolution_function_is_affine_multivariate_p (const_tree chrec, int loopnum)
  888. {
  889. if (chrec == NULL_TREE)
  890. return false;
  891. switch (TREE_CODE (chrec))
  892. {
  893. case POLYNOMIAL_CHREC:
  894. if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec), loopnum))
  895. {
  896. if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum))
  897. return true;
  898. else
  899. {
  900. if (TREE_CODE (CHREC_RIGHT (chrec)) == POLYNOMIAL_CHREC
  901. && CHREC_VARIABLE (CHREC_RIGHT (chrec))
  902. != CHREC_VARIABLE (chrec)
  903. && evolution_function_is_affine_multivariate_p
  904. (CHREC_RIGHT (chrec), loopnum))
  905. return true;
  906. else
  907. return false;
  908. }
  909. }
  910. else
  911. {
  912. if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum)
  913. && TREE_CODE (CHREC_LEFT (chrec)) == POLYNOMIAL_CHREC
  914. && CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec)
  915. && evolution_function_is_affine_multivariate_p
  916. (CHREC_LEFT (chrec), loopnum))
  917. return true;
  918. else
  919. return false;
  920. }
  921. default:
  922. return false;
  923. }
  924. }
  925. /* Determine whether the given tree is a function in zero or one
  926. variables. */
  927. bool
  928. evolution_function_is_univariate_p (const_tree chrec)
  929. {
  930. if (chrec == NULL_TREE)
  931. return true;
  932. switch (TREE_CODE (chrec))
  933. {
  934. case POLYNOMIAL_CHREC:
  935. switch (TREE_CODE (CHREC_LEFT (chrec)))
  936. {
  937. case POLYNOMIAL_CHREC:
  938. if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (CHREC_LEFT (chrec)))
  939. return false;
  940. if (!evolution_function_is_univariate_p (CHREC_LEFT (chrec)))
  941. return false;
  942. break;
  943. default:
  944. if (tree_contains_chrecs (CHREC_LEFT (chrec), NULL))
  945. return false;
  946. break;
  947. }
  948. switch (TREE_CODE (CHREC_RIGHT (chrec)))
  949. {
  950. case POLYNOMIAL_CHREC:
  951. if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (CHREC_RIGHT (chrec)))
  952. return false;
  953. if (!evolution_function_is_univariate_p (CHREC_RIGHT (chrec)))
  954. return false;
  955. break;
  956. default:
  957. if (tree_contains_chrecs (CHREC_RIGHT (chrec), NULL))
  958. return false;
  959. break;
  960. }
  961. default:
  962. return true;
  963. }
  964. }
  965. /* Returns the number of variables of CHREC. Example: the call
  966. nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
  967. unsigned
  968. nb_vars_in_chrec (tree chrec)
  969. {
  970. if (chrec == NULL_TREE)
  971. return 0;
  972. switch (TREE_CODE (chrec))
  973. {
  974. case POLYNOMIAL_CHREC:
  975. return 1 + nb_vars_in_chrec
  976. (initial_condition_in_loop_num (chrec, CHREC_VARIABLE (chrec)));
  977. default:
  978. return 0;
  979. }
  980. }
  981. static tree chrec_convert_1 (tree, tree, gimple, bool);
  982. /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv
  983. the scev corresponds to. AT_STMT is the statement at that the scev is
  984. evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume that
  985. the rules for overflow of the given language apply (e.g., that signed
  986. arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
  987. tests, but also to enforce that the result follows them. Returns true if the
  988. conversion succeeded, false otherwise. */
  989. bool
  990. convert_affine_scev (struct loop *loop, tree type,
  991. tree *base, tree *step, gimple at_stmt,
  992. bool use_overflow_semantics)
  993. {
  994. tree ct = TREE_TYPE (*step);
  995. bool enforce_overflow_semantics;
  996. bool must_check_src_overflow, must_check_rslt_overflow;
  997. tree new_base, new_step;
  998. tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
  999. /* In general,
  1000. (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
  1001. but we must check some assumptions.
  1002. 1) If [BASE, +, STEP] wraps, the equation is not valid when precision
  1003. of CT is smaller than the precision of TYPE. For example, when we
  1004. cast unsigned char [254, +, 1] to unsigned, the values on left side
  1005. are 254, 255, 0, 1, ..., but those on the right side are
  1006. 254, 255, 256, 257, ...
  1007. 2) In case that we must also preserve the fact that signed ivs do not
  1008. overflow, we must additionally check that the new iv does not wrap.
  1009. For example, unsigned char [125, +, 1] casted to signed char could
  1010. become a wrapping variable with values 125, 126, 127, -128, -127, ...,
  1011. which would confuse optimizers that assume that this does not
  1012. happen. */
  1013. must_check_src_overflow = TYPE_PRECISION (ct) < TYPE_PRECISION (type);
  1014. enforce_overflow_semantics = (use_overflow_semantics
  1015. && nowrap_type_p (type));
  1016. if (enforce_overflow_semantics)
  1017. {
  1018. /* We can avoid checking whether the result overflows in the following
  1019. cases:
  1020. -- must_check_src_overflow is true, and the range of TYPE is superset
  1021. of the range of CT -- i.e., in all cases except if CT signed and
  1022. TYPE unsigned.
  1023. -- both CT and TYPE have the same precision and signedness, and we
  1024. verify instead that the source does not overflow (this may be
  1025. easier than verifying it for the result, as we may use the
  1026. information about the semantics of overflow in CT). */
  1027. if (must_check_src_overflow)
  1028. {
  1029. if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (ct))
  1030. must_check_rslt_overflow = true;
  1031. else
  1032. must_check_rslt_overflow = false;
  1033. }
  1034. else if (TYPE_UNSIGNED (ct) == TYPE_UNSIGNED (type)
  1035. && TYPE_PRECISION (ct) == TYPE_PRECISION (type))
  1036. {
  1037. must_check_rslt_overflow = false;
  1038. must_check_src_overflow = true;
  1039. }
  1040. else
  1041. must_check_rslt_overflow = true;
  1042. }
  1043. else
  1044. must_check_rslt_overflow = false;
  1045. if (must_check_src_overflow
  1046. && scev_probably_wraps_p (*base, *step, at_stmt, loop,
  1047. use_overflow_semantics))
  1048. return false;
  1049. new_base = chrec_convert_1 (type, *base, at_stmt,
  1050. use_overflow_semantics);
  1051. /* The step must be sign extended, regardless of the signedness
  1052. of CT and TYPE. This only needs to be handled specially when
  1053. CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
  1054. (with values 100, 99, 98, ...) from becoming signed or unsigned
  1055. [100, +, 255] with values 100, 355, ...; the sign-extension is
  1056. performed by default when CT is signed. */
  1057. new_step = *step;
  1058. if (TYPE_PRECISION (step_type) > TYPE_PRECISION (ct) && TYPE_UNSIGNED (ct))
  1059. {
  1060. tree signed_ct = build_nonstandard_integer_type (TYPE_PRECISION (ct), 0);
  1061. new_step = chrec_convert_1 (signed_ct, new_step, at_stmt,
  1062. use_overflow_semantics);
  1063. }
  1064. new_step = chrec_convert_1 (step_type, new_step, at_stmt, use_overflow_semantics);
  1065. if (automatically_generated_chrec_p (new_base)
  1066. || automatically_generated_chrec_p (new_step))
  1067. return false;
  1068. if (must_check_rslt_overflow
  1069. /* Note that in this case we cannot use the fact that signed variables
  1070. do not overflow, as this is what we are verifying for the new iv. */
  1071. && scev_probably_wraps_p (new_base, new_step, at_stmt, loop, false))
  1072. return false;
  1073. *base = new_base;
  1074. *step = new_step;
  1075. return true;
  1076. }
  1077. /* Convert CHREC for the right hand side of a CHREC.
  1078. The increment for a pointer type is always sizetype. */
  1079. tree
  1080. chrec_convert_rhs (tree type, tree chrec, gimple at_stmt)
  1081. {
  1082. if (POINTER_TYPE_P (type))
  1083. type = sizetype;
  1084. return chrec_convert (type, chrec, at_stmt);
  1085. }
  1086. /* Convert CHREC to TYPE. When the analyzer knows the context in
  1087. which the CHREC is built, it sets AT_STMT to the statement that
  1088. contains the definition of the analyzed variable, otherwise the
  1089. conversion is less accurate: the information is used for
  1090. determining a more accurate estimation of the number of iterations.
  1091. By default AT_STMT could be safely set to NULL_TREE.
  1092. The following rule is always true: TREE_TYPE (chrec) ==
  1093. TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
  1094. An example of what could happen when adding two chrecs and the type
  1095. of the CHREC_RIGHT is different than CHREC_LEFT is:
  1096. {(uint) 0, +, (uchar) 10} +
  1097. {(uint) 0, +, (uchar) 250}
  1098. that would produce a wrong result if CHREC_RIGHT is not (uint):
  1099. {(uint) 0, +, (uchar) 4}
  1100. instead of
  1101. {(uint) 0, +, (uint) 260}
  1102. */
  1103. tree
  1104. chrec_convert (tree type, tree chrec, gimple at_stmt)
  1105. {
  1106. return chrec_convert_1 (type, chrec, at_stmt, true);
  1107. }
  1108. /* Convert CHREC to TYPE. When the analyzer knows the context in
  1109. which the CHREC is built, it sets AT_STMT to the statement that
  1110. contains the definition of the analyzed variable, otherwise the
  1111. conversion is less accurate: the information is used for
  1112. determining a more accurate estimation of the number of iterations.
  1113. By default AT_STMT could be safely set to NULL_TREE.
  1114. USE_OVERFLOW_SEMANTICS is true if this function should assume that
  1115. the rules for overflow of the given language apply (e.g., that signed
  1116. arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
  1117. tests, but also to enforce that the result follows them. */
  1118. static tree
  1119. chrec_convert_1 (tree type, tree chrec, gimple at_stmt,
  1120. bool use_overflow_semantics)
  1121. {
  1122. tree ct, res;
  1123. tree base, step;
  1124. struct loop *loop;
  1125. if (automatically_generated_chrec_p (chrec))
  1126. return chrec;
  1127. ct = chrec_type (chrec);
  1128. if (useless_type_conversion_p (type, ct))
  1129. return chrec;
  1130. if (!evolution_function_is_affine_p (chrec))
  1131. goto keep_cast;
  1132. loop = get_chrec_loop (chrec);
  1133. base = CHREC_LEFT (chrec);
  1134. step = CHREC_RIGHT (chrec);
  1135. if (convert_affine_scev (loop, type, &base, &step, at_stmt,
  1136. use_overflow_semantics))
  1137. return build_polynomial_chrec (loop->num, base, step);
  1138. /* If we cannot propagate the cast inside the chrec, just keep the cast. */
  1139. keep_cast:
  1140. /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
  1141. may be more expensive. We do want to perform this optimization here
  1142. though for canonicalization reasons. */
  1143. if (use_overflow_semantics
  1144. && (TREE_CODE (chrec) == PLUS_EXPR
  1145. || TREE_CODE (chrec) == MINUS_EXPR)
  1146. && TREE_CODE (type) == INTEGER_TYPE
  1147. && TREE_CODE (ct) == INTEGER_TYPE
  1148. && TYPE_PRECISION (type) > TYPE_PRECISION (ct)
  1149. && TYPE_OVERFLOW_UNDEFINED (ct))
  1150. res = fold_build2 (TREE_CODE (chrec), type,
  1151. fold_convert (type, TREE_OPERAND (chrec, 0)),
  1152. fold_convert (type, TREE_OPERAND (chrec, 1)));
  1153. /* Similar perform the trick that (signed char)((int)x + 2) can be
  1154. narrowed to (signed char)((unsigned char)x + 2). */
  1155. else if (use_overflow_semantics
  1156. && TREE_CODE (chrec) == POLYNOMIAL_CHREC
  1157. && TREE_CODE (ct) == INTEGER_TYPE
  1158. && TREE_CODE (type) == INTEGER_TYPE
  1159. && TYPE_OVERFLOW_UNDEFINED (type)
  1160. && TYPE_PRECISION (type) < TYPE_PRECISION (ct))
  1161. {
  1162. tree utype = unsigned_type_for (type);
  1163. res = build_polynomial_chrec (CHREC_VARIABLE (chrec),
  1164. fold_convert (utype,
  1165. CHREC_LEFT (chrec)),
  1166. fold_convert (utype,
  1167. CHREC_RIGHT (chrec)));
  1168. res = chrec_convert_1 (type, res, at_stmt, use_overflow_semantics);
  1169. }
  1170. else
  1171. res = fold_convert (type, chrec);
  1172. /* Don't propagate overflows. */
  1173. if (CONSTANT_CLASS_P (res))
  1174. TREE_OVERFLOW (res) = 0;
  1175. /* But reject constants that don't fit in their type after conversion.
  1176. This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
  1177. natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
  1178. and can cause problems later when computing niters of loops. Note
  1179. that we don't do the check before converting because we don't want
  1180. to reject conversions of negative chrecs to unsigned types. */
  1181. if (TREE_CODE (res) == INTEGER_CST
  1182. && TREE_CODE (type) == INTEGER_TYPE
  1183. && !int_fits_type_p (res, type))
  1184. res = chrec_dont_know;
  1185. return res;
  1186. }
  1187. /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new
  1188. chrec if something else than what chrec_convert would do happens, NULL_TREE
  1189. otherwise. */
  1190. tree
  1191. chrec_convert_aggressive (tree type, tree chrec)
  1192. {
  1193. tree inner_type, left, right, lc, rc, rtype;
  1194. if (automatically_generated_chrec_p (chrec)
  1195. || TREE_CODE (chrec) != POLYNOMIAL_CHREC)
  1196. return NULL_TREE;
  1197. inner_type = TREE_TYPE (chrec);
  1198. if (TYPE_PRECISION (type) > TYPE_PRECISION (inner_type))
  1199. return NULL_TREE;
  1200. rtype = POINTER_TYPE_P (type) ? sizetype : type;
  1201. left = CHREC_LEFT (chrec);
  1202. right = CHREC_RIGHT (chrec);
  1203. lc = chrec_convert_aggressive (type, left);
  1204. if (!lc)
  1205. lc = chrec_convert (type, left, NULL);
  1206. rc = chrec_convert_aggressive (rtype, right);
  1207. if (!rc)
  1208. rc = chrec_convert (rtype, right, NULL);
  1209. return build_polynomial_chrec (CHREC_VARIABLE (chrec), lc, rc);
  1210. }
  1211. /* Returns true when CHREC0 == CHREC1. */
  1212. bool
  1213. eq_evolutions_p (const_tree chrec0, const_tree chrec1)
  1214. {
  1215. if (chrec0 == NULL_TREE
  1216. || chrec1 == NULL_TREE
  1217. || TREE_CODE (chrec0) != TREE_CODE (chrec1))
  1218. return false;
  1219. if (chrec0 == chrec1)
  1220. return true;
  1221. switch (TREE_CODE (chrec0))
  1222. {
  1223. case INTEGER_CST:
  1224. return operand_equal_p (chrec0, chrec1, 0);
  1225. case POLYNOMIAL_CHREC:
  1226. return (CHREC_VARIABLE (chrec0) == CHREC_VARIABLE (chrec1)
  1227. && eq_evolutions_p (CHREC_LEFT (chrec0), CHREC_LEFT (chrec1))
  1228. && eq_evolutions_p (CHREC_RIGHT (chrec0), CHREC_RIGHT (chrec1)));
  1229. case PLUS_EXPR:
  1230. case MULT_EXPR:
  1231. case MINUS_EXPR:
  1232. case POINTER_PLUS_EXPR:
  1233. return eq_evolutions_p (TREE_OPERAND (chrec0, 0),
  1234. TREE_OPERAND (chrec1, 0))
  1235. && eq_evolutions_p (TREE_OPERAND (chrec0, 1),
  1236. TREE_OPERAND (chrec1, 1));
  1237. default:
  1238. return false;
  1239. }
  1240. }
  1241. /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
  1242. EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
  1243. which of these cases happens. */
  1244. enum ev_direction
  1245. scev_direction (const_tree chrec)
  1246. {
  1247. const_tree step;
  1248. if (!evolution_function_is_affine_p (chrec))
  1249. return EV_DIR_UNKNOWN;
  1250. step = CHREC_RIGHT (chrec);
  1251. if (TREE_CODE (step) != INTEGER_CST)
  1252. return EV_DIR_UNKNOWN;
  1253. if (tree_int_cst_sign_bit (step))
  1254. return EV_DIR_DECREASES;
  1255. else
  1256. return EV_DIR_GROWS;
  1257. }
  1258. /* Iterates over all the components of SCEV, and calls CBCK. */
  1259. void
  1260. for_each_scev_op (tree *scev, bool (*cbck) (tree *, void *), void *data)
  1261. {
  1262. switch (TREE_CODE_LENGTH (TREE_CODE (*scev)))
  1263. {
  1264. case 3:
  1265. for_each_scev_op (&TREE_OPERAND (*scev, 2), cbck, data);
  1266. case 2:
  1267. for_each_scev_op (&TREE_OPERAND (*scev, 1), cbck, data);
  1268. case 1:
  1269. for_each_scev_op (&TREE_OPERAND (*scev, 0), cbck, data);
  1270. default:
  1271. cbck (scev, data);
  1272. break;
  1273. }
  1274. }
  1275. /* Returns true when the operation can be part of a linear
  1276. expression. */
  1277. static inline bool
  1278. operator_is_linear (tree scev)
  1279. {
  1280. switch (TREE_CODE (scev))
  1281. {
  1282. case INTEGER_CST:
  1283. case POLYNOMIAL_CHREC:
  1284. case PLUS_EXPR:
  1285. case POINTER_PLUS_EXPR:
  1286. case MULT_EXPR:
  1287. case MINUS_EXPR:
  1288. case NEGATE_EXPR:
  1289. case SSA_NAME:
  1290. case NON_LVALUE_EXPR:
  1291. case BIT_NOT_EXPR:
  1292. CASE_CONVERT:
  1293. return true;
  1294. default:
  1295. return false;
  1296. }
  1297. }
  1298. /* Return true when SCEV is a linear expression. Linear expressions
  1299. can contain additions, substractions and multiplications.
  1300. Multiplications are restricted to constant scaling: "cst * x". */
  1301. bool
  1302. scev_is_linear_expression (tree scev)
  1303. {
  1304. if (scev == NULL
  1305. || !operator_is_linear (scev))
  1306. return false;
  1307. if (TREE_CODE (scev) == MULT_EXPR)
  1308. return !(tree_contains_chrecs (TREE_OPERAND (scev, 0), NULL)
  1309. && tree_contains_chrecs (TREE_OPERAND (scev, 1), NULL));
  1310. if (TREE_CODE (scev) == POLYNOMIAL_CHREC
  1311. && !evolution_function_is_affine_multivariate_p (scev, CHREC_VARIABLE (scev)))
  1312. return false;
  1313. switch (TREE_CODE_LENGTH (TREE_CODE (scev)))
  1314. {
  1315. case 3:
  1316. return scev_is_linear_expression (TREE_OPERAND (scev, 0))
  1317. && scev_is_linear_expression (TREE_OPERAND (scev, 1))
  1318. && scev_is_linear_expression (TREE_OPERAND (scev, 2));
  1319. case 2:
  1320. return scev_is_linear_expression (TREE_OPERAND (scev, 0))
  1321. && scev_is_linear_expression (TREE_OPERAND (scev, 1));
  1322. case 1:
  1323. return scev_is_linear_expression (TREE_OPERAND (scev, 0));
  1324. case 0:
  1325. return true;
  1326. default:
  1327. return false;
  1328. }
  1329. }
  1330. /* Determines whether the expression CHREC contains only interger consts
  1331. in the right parts. */
  1332. bool
  1333. evolution_function_right_is_integer_cst (const_tree chrec)
  1334. {
  1335. if (chrec == NULL_TREE)
  1336. return false;
  1337. switch (TREE_CODE (chrec))
  1338. {
  1339. case INTEGER_CST:
  1340. return true;
  1341. case POLYNOMIAL_CHREC:
  1342. return TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST
  1343. && (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC
  1344. || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec)));
  1345. CASE_CONVERT:
  1346. return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec, 0));
  1347. default:
  1348. return false;
  1349. }
  1350. }