loop-unroll.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. /* Loop unrolling.
  2. Copyright (C) 2002-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 "tm.h"
  19. #include "rtl.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "hard-reg-set.h"
  31. #include "obstack.h"
  32. #include "profile.h"
  33. #include "predict.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "cfgrtl.h"
  38. #include "basic-block.h"
  39. #include "cfgloop.h"
  40. #include "params.h"
  41. #include "insn-codes.h"
  42. #include "optabs.h"
  43. #include "hashtab.h"
  44. #include "flags.h"
  45. #include "statistics.h"
  46. #include "real.h"
  47. #include "fixed-value.h"
  48. #include "insn-config.h"
  49. #include "expmed.h"
  50. #include "dojump.h"
  51. #include "explow.h"
  52. #include "calls.h"
  53. #include "emit-rtl.h"
  54. #include "varasm.h"
  55. #include "stmt.h"
  56. #include "expr.h"
  57. #include "hash-table.h"
  58. #include "recog.h"
  59. #include "target.h"
  60. #include "dumpfile.h"
  61. /* This pass performs loop unrolling. We only perform this
  62. optimization on innermost loops (with single exception) because
  63. the impact on performance is greatest here, and we want to avoid
  64. unnecessary code size growth. The gain is caused by greater sequentiality
  65. of code, better code to optimize for further passes and in some cases
  66. by fewer testings of exit conditions. The main problem is code growth,
  67. that impacts performance negatively due to effect of caches.
  68. What we do:
  69. -- unrolling of loops that roll constant times; this is almost always
  70. win, as we get rid of exit condition tests.
  71. -- unrolling of loops that roll number of times that we can compute
  72. in runtime; we also get rid of exit condition tests here, but there
  73. is the extra expense for calculating the number of iterations
  74. -- simple unrolling of remaining loops; this is performed only if we
  75. are asked to, as the gain is questionable in this case and often
  76. it may even slow down the code
  77. For more detailed descriptions of each of those, see comments at
  78. appropriate function below.
  79. There is a lot of parameters (defined and described in params.def) that
  80. control how much we unroll.
  81. ??? A great problem is that we don't have a good way how to determine
  82. how many times we should unroll the loop; the experiments I have made
  83. showed that this choice may affect performance in order of several %.
  84. */
  85. /* Information about induction variables to split. */
  86. struct iv_to_split
  87. {
  88. rtx_insn *insn; /* The insn in that the induction variable occurs. */
  89. rtx orig_var; /* The variable (register) for the IV before split. */
  90. rtx base_var; /* The variable on that the values in the further
  91. iterations are based. */
  92. rtx step; /* Step of the induction variable. */
  93. struct iv_to_split *next; /* Next entry in walking order. */
  94. };
  95. /* Information about accumulators to expand. */
  96. struct var_to_expand
  97. {
  98. rtx_insn *insn; /* The insn in that the variable expansion occurs. */
  99. rtx reg; /* The accumulator which is expanded. */
  100. vec<rtx> var_expansions; /* The copies of the accumulator which is expanded. */
  101. struct var_to_expand *next; /* Next entry in walking order. */
  102. enum rtx_code op; /* The type of the accumulation - addition, subtraction
  103. or multiplication. */
  104. int expansion_count; /* Count the number of expansions generated so far. */
  105. int reuse_expansion; /* The expansion we intend to reuse to expand
  106. the accumulator. If REUSE_EXPANSION is 0 reuse
  107. the original accumulator. Else use
  108. var_expansions[REUSE_EXPANSION - 1]. */
  109. };
  110. /* Hashtable helper for iv_to_split. */
  111. struct iv_split_hasher : typed_free_remove <iv_to_split>
  112. {
  113. typedef iv_to_split value_type;
  114. typedef iv_to_split compare_type;
  115. static inline hashval_t hash (const value_type *);
  116. static inline bool equal (const value_type *, const compare_type *);
  117. };
  118. /* A hash function for information about insns to split. */
  119. inline hashval_t
  120. iv_split_hasher::hash (const value_type *ivts)
  121. {
  122. return (hashval_t) INSN_UID (ivts->insn);
  123. }
  124. /* An equality functions for information about insns to split. */
  125. inline bool
  126. iv_split_hasher::equal (const value_type *i1, const compare_type *i2)
  127. {
  128. return i1->insn == i2->insn;
  129. }
  130. /* Hashtable helper for iv_to_split. */
  131. struct var_expand_hasher : typed_free_remove <var_to_expand>
  132. {
  133. typedef var_to_expand value_type;
  134. typedef var_to_expand compare_type;
  135. static inline hashval_t hash (const value_type *);
  136. static inline bool equal (const value_type *, const compare_type *);
  137. };
  138. /* Return a hash for VES. */
  139. inline hashval_t
  140. var_expand_hasher::hash (const value_type *ves)
  141. {
  142. return (hashval_t) INSN_UID (ves->insn);
  143. }
  144. /* Return true if I1 and I2 refer to the same instruction. */
  145. inline bool
  146. var_expand_hasher::equal (const value_type *i1, const compare_type *i2)
  147. {
  148. return i1->insn == i2->insn;
  149. }
  150. /* Information about optimization applied in
  151. the unrolled loop. */
  152. struct opt_info
  153. {
  154. hash_table<iv_split_hasher> *insns_to_split; /* A hashtable of insns to
  155. split. */
  156. struct iv_to_split *iv_to_split_head; /* The first iv to split. */
  157. struct iv_to_split **iv_to_split_tail; /* Pointer to the tail of the list. */
  158. hash_table<var_expand_hasher> *insns_with_var_to_expand; /* A hashtable of
  159. insns with accumulators to expand. */
  160. struct var_to_expand *var_to_expand_head; /* The first var to expand. */
  161. struct var_to_expand **var_to_expand_tail; /* Pointer to the tail of the list. */
  162. unsigned first_new_block; /* The first basic block that was
  163. duplicated. */
  164. basic_block loop_exit; /* The loop exit basic block. */
  165. basic_block loop_preheader; /* The loop preheader basic block. */
  166. };
  167. static void decide_unroll_stupid (struct loop *, int);
  168. static void decide_unroll_constant_iterations (struct loop *, int);
  169. static void decide_unroll_runtime_iterations (struct loop *, int);
  170. static void unroll_loop_stupid (struct loop *);
  171. static void decide_unrolling (int);
  172. static void unroll_loop_constant_iterations (struct loop *);
  173. static void unroll_loop_runtime_iterations (struct loop *);
  174. static struct opt_info *analyze_insns_in_loop (struct loop *);
  175. static void opt_info_start_duplication (struct opt_info *);
  176. static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool);
  177. static void free_opt_info (struct opt_info *);
  178. static struct var_to_expand *analyze_insn_to_expand_var (struct loop*, rtx_insn *);
  179. static bool referenced_in_one_insn_in_loop_p (struct loop *, rtx, int *);
  180. static struct iv_to_split *analyze_iv_to_split_insn (rtx_insn *);
  181. static void expand_var_during_unrolling (struct var_to_expand *, rtx_insn *);
  182. static void insert_var_expansion_initialization (struct var_to_expand *,
  183. basic_block);
  184. static void combine_var_copies_in_loop_exit (struct var_to_expand *,
  185. basic_block);
  186. static rtx get_expansion (struct var_to_expand *);
  187. /* Emit a message summarizing the unroll that will be
  188. performed for LOOP, along with the loop's location LOCUS, if
  189. appropriate given the dump or -fopt-info settings. */
  190. static void
  191. report_unroll (struct loop *loop, location_t locus)
  192. {
  193. int report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_RTL | TDF_DETAILS;
  194. if (loop->lpt_decision.decision == LPT_NONE)
  195. return;
  196. if (!dump_enabled_p ())
  197. return;
  198. dump_printf_loc (report_flags, locus,
  199. "loop unrolled %d times",
  200. loop->lpt_decision.times);
  201. if (profile_info)
  202. dump_printf (report_flags,
  203. " (header execution count %d)",
  204. (int)loop->header->count);
  205. dump_printf (report_flags, "\n");
  206. }
  207. /* Decide whether unroll loops and how much. */
  208. static void
  209. decide_unrolling (int flags)
  210. {
  211. struct loop *loop;
  212. /* Scan the loops, inner ones first. */
  213. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  214. {
  215. loop->lpt_decision.decision = LPT_NONE;
  216. location_t locus = get_loop_location (loop);
  217. if (dump_enabled_p ())
  218. dump_printf_loc (TDF_RTL, locus,
  219. ";; *** Considering loop %d at BB %d for "
  220. "unrolling ***\n",
  221. loop->num, loop->header->index);
  222. /* Do not peel cold areas. */
  223. if (optimize_loop_for_size_p (loop))
  224. {
  225. if (dump_file)
  226. fprintf (dump_file, ";; Not considering loop, cold area\n");
  227. continue;
  228. }
  229. /* Can the loop be manipulated? */
  230. if (!can_duplicate_loop_p (loop))
  231. {
  232. if (dump_file)
  233. fprintf (dump_file,
  234. ";; Not considering loop, cannot duplicate\n");
  235. continue;
  236. }
  237. /* Skip non-innermost loops. */
  238. if (loop->inner)
  239. {
  240. if (dump_file)
  241. fprintf (dump_file, ";; Not considering loop, is not innermost\n");
  242. continue;
  243. }
  244. loop->ninsns = num_loop_insns (loop);
  245. loop->av_ninsns = average_num_loop_insns (loop);
  246. /* Try transformations one by one in decreasing order of
  247. priority. */
  248. decide_unroll_constant_iterations (loop, flags);
  249. if (loop->lpt_decision.decision == LPT_NONE)
  250. decide_unroll_runtime_iterations (loop, flags);
  251. if (loop->lpt_decision.decision == LPT_NONE)
  252. decide_unroll_stupid (loop, flags);
  253. report_unroll (loop, locus);
  254. }
  255. }
  256. /* Unroll LOOPS. */
  257. void
  258. unroll_loops (int flags)
  259. {
  260. struct loop *loop;
  261. bool changed = false;
  262. /* Now decide rest of unrolling. */
  263. decide_unrolling (flags);
  264. /* Scan the loops, inner ones first. */
  265. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  266. {
  267. /* And perform the appropriate transformations. */
  268. switch (loop->lpt_decision.decision)
  269. {
  270. case LPT_UNROLL_CONSTANT:
  271. unroll_loop_constant_iterations (loop);
  272. changed = true;
  273. break;
  274. case LPT_UNROLL_RUNTIME:
  275. unroll_loop_runtime_iterations (loop);
  276. changed = true;
  277. break;
  278. case LPT_UNROLL_STUPID:
  279. unroll_loop_stupid (loop);
  280. changed = true;
  281. break;
  282. case LPT_NONE:
  283. break;
  284. default:
  285. gcc_unreachable ();
  286. }
  287. }
  288. if (changed)
  289. {
  290. calculate_dominance_info (CDI_DOMINATORS);
  291. fix_loop_structure (NULL);
  292. }
  293. iv_analysis_done ();
  294. }
  295. /* Check whether exit of the LOOP is at the end of loop body. */
  296. static bool
  297. loop_exit_at_end_p (struct loop *loop)
  298. {
  299. struct niter_desc *desc = get_simple_loop_desc (loop);
  300. rtx_insn *insn;
  301. /* We should never have conditional in latch block. */
  302. gcc_assert (desc->in_edge->dest != loop->header);
  303. if (desc->in_edge->dest != loop->latch)
  304. return false;
  305. /* Check that the latch is empty. */
  306. FOR_BB_INSNS (loop->latch, insn)
  307. {
  308. if (INSN_P (insn) && active_insn_p (insn))
  309. return false;
  310. }
  311. return true;
  312. }
  313. /* Decide whether to unroll LOOP iterating constant number of times
  314. and how much. */
  315. static void
  316. decide_unroll_constant_iterations (struct loop *loop, int flags)
  317. {
  318. unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
  319. struct niter_desc *desc;
  320. widest_int iterations;
  321. if (!(flags & UAP_UNROLL))
  322. {
  323. /* We were not asked to, just return back silently. */
  324. return;
  325. }
  326. if (dump_file)
  327. fprintf (dump_file,
  328. "\n;; Considering unrolling loop with constant "
  329. "number of iterations\n");
  330. /* nunroll = total number of copies of the original loop body in
  331. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  332. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  333. nunroll_by_av
  334. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  335. if (nunroll > nunroll_by_av)
  336. nunroll = nunroll_by_av;
  337. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  338. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  339. if (targetm.loop_unroll_adjust)
  340. nunroll = targetm.loop_unroll_adjust (nunroll, loop);
  341. /* Skip big loops. */
  342. if (nunroll <= 1)
  343. {
  344. if (dump_file)
  345. fprintf (dump_file, ";; Not considering loop, is too big\n");
  346. return;
  347. }
  348. /* Check for simple loops. */
  349. desc = get_simple_loop_desc (loop);
  350. /* Check number of iterations. */
  351. if (!desc->simple_p || !desc->const_iter || desc->assumptions)
  352. {
  353. if (dump_file)
  354. fprintf (dump_file,
  355. ";; Unable to prove that the loop iterates constant times\n");
  356. return;
  357. }
  358. /* Check whether the loop rolls enough to consider.
  359. Consult also loop bounds and profile; in the case the loop has more
  360. than one exit it may well loop less than determined maximal number
  361. of iterations. */
  362. if (desc->niter < 2 * nunroll
  363. || ((get_estimated_loop_iterations (loop, &iterations)
  364. || get_max_loop_iterations (loop, &iterations))
  365. && wi::ltu_p (iterations, 2 * nunroll)))
  366. {
  367. if (dump_file)
  368. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  369. return;
  370. }
  371. /* Success; now compute number of iterations to unroll. We alter
  372. nunroll so that as few as possible copies of loop body are
  373. necessary, while still not decreasing the number of unrollings
  374. too much (at most by 1). */
  375. best_copies = 2 * nunroll + 10;
  376. i = 2 * nunroll + 2;
  377. if (i - 1 >= desc->niter)
  378. i = desc->niter - 2;
  379. for (; i >= nunroll - 1; i--)
  380. {
  381. unsigned exit_mod = desc->niter % (i + 1);
  382. if (!loop_exit_at_end_p (loop))
  383. n_copies = exit_mod + i + 1;
  384. else if (exit_mod != (unsigned) i
  385. || desc->noloop_assumptions != NULL_RTX)
  386. n_copies = exit_mod + i + 2;
  387. else
  388. n_copies = i + 1;
  389. if (n_copies < best_copies)
  390. {
  391. best_copies = n_copies;
  392. best_unroll = i;
  393. }
  394. }
  395. loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
  396. loop->lpt_decision.times = best_unroll;
  397. }
  398. /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
  399. The transformation does this:
  400. for (i = 0; i < 102; i++)
  401. body;
  402. ==> (LOOP->LPT_DECISION.TIMES == 3)
  403. i = 0;
  404. body; i++;
  405. body; i++;
  406. while (i < 102)
  407. {
  408. body; i++;
  409. body; i++;
  410. body; i++;
  411. body; i++;
  412. }
  413. */
  414. static void
  415. unroll_loop_constant_iterations (struct loop *loop)
  416. {
  417. unsigned HOST_WIDE_INT niter;
  418. unsigned exit_mod;
  419. sbitmap wont_exit;
  420. unsigned i;
  421. edge e;
  422. unsigned max_unroll = loop->lpt_decision.times;
  423. struct niter_desc *desc = get_simple_loop_desc (loop);
  424. bool exit_at_end = loop_exit_at_end_p (loop);
  425. struct opt_info *opt_info = NULL;
  426. bool ok;
  427. niter = desc->niter;
  428. /* Should not get here (such loop should be peeled instead). */
  429. gcc_assert (niter > max_unroll + 1);
  430. exit_mod = niter % (max_unroll + 1);
  431. wont_exit = sbitmap_alloc (max_unroll + 1);
  432. bitmap_ones (wont_exit);
  433. auto_vec<edge> remove_edges;
  434. if (flag_split_ivs_in_unroller
  435. || flag_variable_expansion_in_unroller)
  436. opt_info = analyze_insns_in_loop (loop);
  437. if (!exit_at_end)
  438. {
  439. /* The exit is not at the end of the loop; leave exit test
  440. in the first copy, so that the loops that start with test
  441. of exit condition have continuous body after unrolling. */
  442. if (dump_file)
  443. fprintf (dump_file, ";; Condition at beginning of loop.\n");
  444. /* Peel exit_mod iterations. */
  445. bitmap_clear_bit (wont_exit, 0);
  446. if (desc->noloop_assumptions)
  447. bitmap_clear_bit (wont_exit, 1);
  448. if (exit_mod)
  449. {
  450. opt_info_start_duplication (opt_info);
  451. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  452. exit_mod,
  453. wont_exit, desc->out_edge,
  454. &remove_edges,
  455. DLTHE_FLAG_UPDATE_FREQ
  456. | (opt_info && exit_mod > 1
  457. ? DLTHE_RECORD_COPY_NUMBER
  458. : 0));
  459. gcc_assert (ok);
  460. if (opt_info && exit_mod > 1)
  461. apply_opt_in_copies (opt_info, exit_mod, false, false);
  462. desc->noloop_assumptions = NULL_RTX;
  463. desc->niter -= exit_mod;
  464. loop->nb_iterations_upper_bound -= exit_mod;
  465. if (loop->any_estimate
  466. && wi::leu_p (exit_mod, loop->nb_iterations_estimate))
  467. loop->nb_iterations_estimate -= exit_mod;
  468. else
  469. loop->any_estimate = false;
  470. }
  471. bitmap_set_bit (wont_exit, 1);
  472. }
  473. else
  474. {
  475. /* Leave exit test in last copy, for the same reason as above if
  476. the loop tests the condition at the end of loop body. */
  477. if (dump_file)
  478. fprintf (dump_file, ";; Condition at end of loop.\n");
  479. /* We know that niter >= max_unroll + 2; so we do not need to care of
  480. case when we would exit before reaching the loop. So just peel
  481. exit_mod + 1 iterations. */
  482. if (exit_mod != max_unroll
  483. || desc->noloop_assumptions)
  484. {
  485. bitmap_clear_bit (wont_exit, 0);
  486. if (desc->noloop_assumptions)
  487. bitmap_clear_bit (wont_exit, 1);
  488. opt_info_start_duplication (opt_info);
  489. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  490. exit_mod + 1,
  491. wont_exit, desc->out_edge,
  492. &remove_edges,
  493. DLTHE_FLAG_UPDATE_FREQ
  494. | (opt_info && exit_mod > 0
  495. ? DLTHE_RECORD_COPY_NUMBER
  496. : 0));
  497. gcc_assert (ok);
  498. if (opt_info && exit_mod > 0)
  499. apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
  500. desc->niter -= exit_mod + 1;
  501. loop->nb_iterations_upper_bound -= exit_mod + 1;
  502. if (loop->any_estimate
  503. && wi::leu_p (exit_mod + 1, loop->nb_iterations_estimate))
  504. loop->nb_iterations_estimate -= exit_mod + 1;
  505. else
  506. loop->any_estimate = false;
  507. desc->noloop_assumptions = NULL_RTX;
  508. bitmap_set_bit (wont_exit, 0);
  509. bitmap_set_bit (wont_exit, 1);
  510. }
  511. bitmap_clear_bit (wont_exit, max_unroll);
  512. }
  513. /* Now unroll the loop. */
  514. opt_info_start_duplication (opt_info);
  515. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  516. max_unroll,
  517. wont_exit, desc->out_edge,
  518. &remove_edges,
  519. DLTHE_FLAG_UPDATE_FREQ
  520. | (opt_info
  521. ? DLTHE_RECORD_COPY_NUMBER
  522. : 0));
  523. gcc_assert (ok);
  524. if (opt_info)
  525. {
  526. apply_opt_in_copies (opt_info, max_unroll, true, true);
  527. free_opt_info (opt_info);
  528. }
  529. free (wont_exit);
  530. if (exit_at_end)
  531. {
  532. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  533. /* Find a new in and out edge; they are in the last copy we have made. */
  534. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  535. {
  536. desc->out_edge = EDGE_SUCC (exit_block, 0);
  537. desc->in_edge = EDGE_SUCC (exit_block, 1);
  538. }
  539. else
  540. {
  541. desc->out_edge = EDGE_SUCC (exit_block, 1);
  542. desc->in_edge = EDGE_SUCC (exit_block, 0);
  543. }
  544. }
  545. desc->niter /= max_unroll + 1;
  546. loop->nb_iterations_upper_bound
  547. = wi::udiv_trunc (loop->nb_iterations_upper_bound, max_unroll + 1);
  548. if (loop->any_estimate)
  549. loop->nb_iterations_estimate
  550. = wi::udiv_trunc (loop->nb_iterations_estimate, max_unroll + 1);
  551. desc->niter_expr = GEN_INT (desc->niter);
  552. /* Remove the edges. */
  553. FOR_EACH_VEC_ELT (remove_edges, i, e)
  554. remove_path (e);
  555. if (dump_file)
  556. fprintf (dump_file,
  557. ";; Unrolled loop %d times, constant # of iterations %i insns\n",
  558. max_unroll, num_loop_insns (loop));
  559. }
  560. /* Decide whether to unroll LOOP iterating runtime computable number of times
  561. and how much. */
  562. static void
  563. decide_unroll_runtime_iterations (struct loop *loop, int flags)
  564. {
  565. unsigned nunroll, nunroll_by_av, i;
  566. struct niter_desc *desc;
  567. widest_int iterations;
  568. if (!(flags & UAP_UNROLL))
  569. {
  570. /* We were not asked to, just return back silently. */
  571. return;
  572. }
  573. if (dump_file)
  574. fprintf (dump_file,
  575. "\n;; Considering unrolling loop with runtime "
  576. "computable number of iterations\n");
  577. /* nunroll = total number of copies of the original loop body in
  578. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  579. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  580. nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  581. if (nunroll > nunroll_by_av)
  582. nunroll = nunroll_by_av;
  583. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  584. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  585. if (targetm.loop_unroll_adjust)
  586. nunroll = targetm.loop_unroll_adjust (nunroll, loop);
  587. /* Skip big loops. */
  588. if (nunroll <= 1)
  589. {
  590. if (dump_file)
  591. fprintf (dump_file, ";; Not considering loop, is too big\n");
  592. return;
  593. }
  594. /* Check for simple loops. */
  595. desc = get_simple_loop_desc (loop);
  596. /* Check simpleness. */
  597. if (!desc->simple_p || desc->assumptions)
  598. {
  599. if (dump_file)
  600. fprintf (dump_file,
  601. ";; Unable to prove that the number of iterations "
  602. "can be counted in runtime\n");
  603. return;
  604. }
  605. if (desc->const_iter)
  606. {
  607. if (dump_file)
  608. fprintf (dump_file, ";; Loop iterates constant times\n");
  609. return;
  610. }
  611. /* Check whether the loop rolls. */
  612. if ((get_estimated_loop_iterations (loop, &iterations)
  613. || get_max_loop_iterations (loop, &iterations))
  614. && wi::ltu_p (iterations, 2 * nunroll))
  615. {
  616. if (dump_file)
  617. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  618. return;
  619. }
  620. /* Success; now force nunroll to be power of 2, as we are unable to
  621. cope with overflows in computation of number of iterations. */
  622. for (i = 1; 2 * i <= nunroll; i *= 2)
  623. continue;
  624. loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
  625. loop->lpt_decision.times = i - 1;
  626. }
  627. /* Splits edge E and inserts the sequence of instructions INSNS on it, and
  628. returns the newly created block. If INSNS is NULL_RTX, nothing is changed
  629. and NULL is returned instead. */
  630. basic_block
  631. split_edge_and_insert (edge e, rtx_insn *insns)
  632. {
  633. basic_block bb;
  634. if (!insns)
  635. return NULL;
  636. bb = split_edge (e);
  637. emit_insn_after (insns, BB_END (bb));
  638. /* ??? We used to assume that INSNS can contain control flow insns, and
  639. that we had to try to find sub basic blocks in BB to maintain a valid
  640. CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
  641. and call break_superblocks when going out of cfglayout mode. But it
  642. turns out that this never happens; and that if it does ever happen,
  643. the verify_flow_info at the end of the RTL loop passes would fail.
  644. There are two reasons why we expected we could have control flow insns
  645. in INSNS. The first is when a comparison has to be done in parts, and
  646. the second is when the number of iterations is computed for loops with
  647. the number of iterations known at runtime. In both cases, test cases
  648. to get control flow in INSNS appear to be impossible to construct:
  649. * If do_compare_rtx_and_jump needs several branches to do comparison
  650. in a mode that needs comparison by parts, we cannot analyze the
  651. number of iterations of the loop, and we never get to unrolling it.
  652. * The code in expand_divmod that was suspected to cause creation of
  653. branching code seems to be only accessed for signed division. The
  654. divisions used by # of iterations analysis are always unsigned.
  655. Problems might arise on architectures that emits branching code
  656. for some operations that may appear in the unroller (especially
  657. for division), but we have no such architectures.
  658. Considering all this, it was decided that we should for now assume
  659. that INSNS can in theory contain control flow insns, but in practice
  660. it never does. So we don't handle the theoretical case, and should
  661. a real failure ever show up, we have a pretty good clue for how to
  662. fix it. */
  663. return bb;
  664. }
  665. /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
  666. true, with probability PROB. If CINSN is not NULL, it is the insn to copy
  667. in order to create a jump. */
  668. static rtx_insn *
  669. compare_and_jump_seq (rtx op0, rtx op1, enum rtx_code comp, rtx label, int prob,
  670. rtx_insn *cinsn)
  671. {
  672. rtx_insn *seq, *jump;
  673. rtx cond;
  674. machine_mode mode;
  675. mode = GET_MODE (op0);
  676. if (mode == VOIDmode)
  677. mode = GET_MODE (op1);
  678. start_sequence ();
  679. if (GET_MODE_CLASS (mode) == MODE_CC)
  680. {
  681. /* A hack -- there seems to be no easy generic way how to make a
  682. conditional jump from a ccmode comparison. */
  683. gcc_assert (cinsn);
  684. cond = XEXP (SET_SRC (pc_set (cinsn)), 0);
  685. gcc_assert (GET_CODE (cond) == comp);
  686. gcc_assert (rtx_equal_p (op0, XEXP (cond, 0)));
  687. gcc_assert (rtx_equal_p (op1, XEXP (cond, 1)));
  688. emit_jump_insn (copy_insn (PATTERN (cinsn)));
  689. jump = get_last_insn ();
  690. gcc_assert (JUMP_P (jump));
  691. JUMP_LABEL (jump) = JUMP_LABEL (cinsn);
  692. LABEL_NUSES (JUMP_LABEL (jump))++;
  693. redirect_jump (jump, label, 0);
  694. }
  695. else
  696. {
  697. gcc_assert (!cinsn);
  698. op0 = force_operand (op0, NULL_RTX);
  699. op1 = force_operand (op1, NULL_RTX);
  700. do_compare_rtx_and_jump (op0, op1, comp, 0,
  701. mode, NULL_RTX, NULL_RTX, label, -1);
  702. jump = get_last_insn ();
  703. gcc_assert (JUMP_P (jump));
  704. JUMP_LABEL (jump) = label;
  705. LABEL_NUSES (label)++;
  706. }
  707. add_int_reg_note (jump, REG_BR_PROB, prob);
  708. seq = get_insns ();
  709. end_sequence ();
  710. return seq;
  711. }
  712. /* Unroll LOOP for which we are able to count number of iterations in runtime
  713. LOOP->LPT_DECISION.TIMES times. The transformation does this (with some
  714. extra care for case n < 0):
  715. for (i = 0; i < n; i++)
  716. body;
  717. ==> (LOOP->LPT_DECISION.TIMES == 3)
  718. i = 0;
  719. mod = n % 4;
  720. switch (mod)
  721. {
  722. case 3:
  723. body; i++;
  724. case 2:
  725. body; i++;
  726. case 1:
  727. body; i++;
  728. case 0: ;
  729. }
  730. while (i < n)
  731. {
  732. body; i++;
  733. body; i++;
  734. body; i++;
  735. body; i++;
  736. }
  737. */
  738. static void
  739. unroll_loop_runtime_iterations (struct loop *loop)
  740. {
  741. rtx old_niter, niter, tmp;
  742. rtx_insn *init_code, *branch_code;
  743. unsigned i, j, p;
  744. basic_block preheader, *body, swtch, ezc_swtch;
  745. sbitmap wont_exit;
  746. int may_exit_copy;
  747. unsigned n_peel;
  748. edge e;
  749. bool extra_zero_check, last_may_exit;
  750. unsigned max_unroll = loop->lpt_decision.times;
  751. struct niter_desc *desc = get_simple_loop_desc (loop);
  752. bool exit_at_end = loop_exit_at_end_p (loop);
  753. struct opt_info *opt_info = NULL;
  754. bool ok;
  755. if (flag_split_ivs_in_unroller
  756. || flag_variable_expansion_in_unroller)
  757. opt_info = analyze_insns_in_loop (loop);
  758. /* Remember blocks whose dominators will have to be updated. */
  759. auto_vec<basic_block> dom_bbs;
  760. body = get_loop_body (loop);
  761. for (i = 0; i < loop->num_nodes; i++)
  762. {
  763. vec<basic_block> ldom;
  764. basic_block bb;
  765. ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
  766. FOR_EACH_VEC_ELT (ldom, j, bb)
  767. if (!flow_bb_inside_loop_p (loop, bb))
  768. dom_bbs.safe_push (bb);
  769. ldom.release ();
  770. }
  771. free (body);
  772. if (!exit_at_end)
  773. {
  774. /* Leave exit in first copy (for explanation why see comment in
  775. unroll_loop_constant_iterations). */
  776. may_exit_copy = 0;
  777. n_peel = max_unroll - 1;
  778. extra_zero_check = true;
  779. last_may_exit = false;
  780. }
  781. else
  782. {
  783. /* Leave exit in last copy (for explanation why see comment in
  784. unroll_loop_constant_iterations). */
  785. may_exit_copy = max_unroll;
  786. n_peel = max_unroll;
  787. extra_zero_check = false;
  788. last_may_exit = true;
  789. }
  790. /* Get expression for number of iterations. */
  791. start_sequence ();
  792. old_niter = niter = gen_reg_rtx (desc->mode);
  793. tmp = force_operand (copy_rtx (desc->niter_expr), niter);
  794. if (tmp != niter)
  795. emit_move_insn (niter, tmp);
  796. /* Count modulo by ANDing it with max_unroll; we use the fact that
  797. the number of unrollings is a power of two, and thus this is correct
  798. even if there is overflow in the computation. */
  799. niter = expand_simple_binop (desc->mode, AND,
  800. niter, gen_int_mode (max_unroll, desc->mode),
  801. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  802. init_code = get_insns ();
  803. end_sequence ();
  804. unshare_all_rtl_in_chain (init_code);
  805. /* Precondition the loop. */
  806. split_edge_and_insert (loop_preheader_edge (loop), init_code);
  807. auto_vec<edge> remove_edges;
  808. wont_exit = sbitmap_alloc (max_unroll + 2);
  809. /* Peel the first copy of loop body (almost always we must leave exit test
  810. here; the only exception is when we have extra zero check and the number
  811. of iterations is reliable. Also record the place of (possible) extra
  812. zero check. */
  813. bitmap_clear (wont_exit);
  814. if (extra_zero_check
  815. && !desc->noloop_assumptions)
  816. bitmap_set_bit (wont_exit, 1);
  817. ezc_swtch = loop_preheader_edge (loop)->src;
  818. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  819. 1, wont_exit, desc->out_edge,
  820. &remove_edges,
  821. DLTHE_FLAG_UPDATE_FREQ);
  822. gcc_assert (ok);
  823. /* Record the place where switch will be built for preconditioning. */
  824. swtch = split_edge (loop_preheader_edge (loop));
  825. for (i = 0; i < n_peel; i++)
  826. {
  827. /* Peel the copy. */
  828. bitmap_clear (wont_exit);
  829. if (i != n_peel - 1 || !last_may_exit)
  830. bitmap_set_bit (wont_exit, 1);
  831. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  832. 1, wont_exit, desc->out_edge,
  833. &remove_edges,
  834. DLTHE_FLAG_UPDATE_FREQ);
  835. gcc_assert (ok);
  836. /* Create item for switch. */
  837. j = n_peel - i - (extra_zero_check ? 0 : 1);
  838. p = REG_BR_PROB_BASE / (i + 2);
  839. preheader = split_edge (loop_preheader_edge (loop));
  840. branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ,
  841. block_label (preheader), p,
  842. NULL);
  843. /* We rely on the fact that the compare and jump cannot be optimized out,
  844. and hence the cfg we create is correct. */
  845. gcc_assert (branch_code != NULL_RTX);
  846. swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code);
  847. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  848. single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  849. e = make_edge (swtch, preheader,
  850. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  851. e->count = RDIV (preheader->count * REG_BR_PROB_BASE, p);
  852. e->probability = p;
  853. }
  854. if (extra_zero_check)
  855. {
  856. /* Add branch for zero iterations. */
  857. p = REG_BR_PROB_BASE / (max_unroll + 1);
  858. swtch = ezc_swtch;
  859. preheader = split_edge (loop_preheader_edge (loop));
  860. branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ,
  861. block_label (preheader), p,
  862. NULL);
  863. gcc_assert (branch_code != NULL_RTX);
  864. swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code);
  865. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  866. single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  867. e = make_edge (swtch, preheader,
  868. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  869. e->count = RDIV (preheader->count * REG_BR_PROB_BASE, p);
  870. e->probability = p;
  871. }
  872. /* Recount dominators for outer blocks. */
  873. iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
  874. /* And unroll loop. */
  875. bitmap_ones (wont_exit);
  876. bitmap_clear_bit (wont_exit, may_exit_copy);
  877. opt_info_start_duplication (opt_info);
  878. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  879. max_unroll,
  880. wont_exit, desc->out_edge,
  881. &remove_edges,
  882. DLTHE_FLAG_UPDATE_FREQ
  883. | (opt_info
  884. ? DLTHE_RECORD_COPY_NUMBER
  885. : 0));
  886. gcc_assert (ok);
  887. if (opt_info)
  888. {
  889. apply_opt_in_copies (opt_info, max_unroll, true, true);
  890. free_opt_info (opt_info);
  891. }
  892. free (wont_exit);
  893. if (exit_at_end)
  894. {
  895. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  896. /* Find a new in and out edge; they are in the last copy we have
  897. made. */
  898. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  899. {
  900. desc->out_edge = EDGE_SUCC (exit_block, 0);
  901. desc->in_edge = EDGE_SUCC (exit_block, 1);
  902. }
  903. else
  904. {
  905. desc->out_edge = EDGE_SUCC (exit_block, 1);
  906. desc->in_edge = EDGE_SUCC (exit_block, 0);
  907. }
  908. }
  909. /* Remove the edges. */
  910. FOR_EACH_VEC_ELT (remove_edges, i, e)
  911. remove_path (e);
  912. /* We must be careful when updating the number of iterations due to
  913. preconditioning and the fact that the value must be valid at entry
  914. of the loop. After passing through the above code, we see that
  915. the correct new number of iterations is this: */
  916. gcc_assert (!desc->const_iter);
  917. desc->niter_expr =
  918. simplify_gen_binary (UDIV, desc->mode, old_niter,
  919. gen_int_mode (max_unroll + 1, desc->mode));
  920. loop->nb_iterations_upper_bound
  921. = wi::udiv_trunc (loop->nb_iterations_upper_bound, max_unroll + 1);
  922. if (loop->any_estimate)
  923. loop->nb_iterations_estimate
  924. = wi::udiv_trunc (loop->nb_iterations_estimate, max_unroll + 1);
  925. if (exit_at_end)
  926. {
  927. desc->niter_expr =
  928. simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx);
  929. desc->noloop_assumptions = NULL_RTX;
  930. --loop->nb_iterations_upper_bound;
  931. if (loop->any_estimate
  932. && loop->nb_iterations_estimate != 0)
  933. --loop->nb_iterations_estimate;
  934. else
  935. loop->any_estimate = false;
  936. }
  937. if (dump_file)
  938. fprintf (dump_file,
  939. ";; Unrolled loop %d times, counting # of iterations "
  940. "in runtime, %i insns\n",
  941. max_unroll, num_loop_insns (loop));
  942. }
  943. /* Decide whether to unroll LOOP stupidly and how much. */
  944. static void
  945. decide_unroll_stupid (struct loop *loop, int flags)
  946. {
  947. unsigned nunroll, nunroll_by_av, i;
  948. struct niter_desc *desc;
  949. widest_int iterations;
  950. if (!(flags & UAP_UNROLL_ALL))
  951. {
  952. /* We were not asked to, just return back silently. */
  953. return;
  954. }
  955. if (dump_file)
  956. fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n");
  957. /* nunroll = total number of copies of the original loop body in
  958. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  959. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  960. nunroll_by_av
  961. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  962. if (nunroll > nunroll_by_av)
  963. nunroll = nunroll_by_av;
  964. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  965. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  966. if (targetm.loop_unroll_adjust)
  967. nunroll = targetm.loop_unroll_adjust (nunroll, loop);
  968. /* Skip big loops. */
  969. if (nunroll <= 1)
  970. {
  971. if (dump_file)
  972. fprintf (dump_file, ";; Not considering loop, is too big\n");
  973. return;
  974. }
  975. /* Check for simple loops. */
  976. desc = get_simple_loop_desc (loop);
  977. /* Check simpleness. */
  978. if (desc->simple_p && !desc->assumptions)
  979. {
  980. if (dump_file)
  981. fprintf (dump_file, ";; The loop is simple\n");
  982. return;
  983. }
  984. /* Do not unroll loops with branches inside -- it increases number
  985. of mispredicts.
  986. TODO: this heuristic needs tunning; call inside the loop body
  987. is also relatively good reason to not unroll. */
  988. if (num_loop_branches (loop) > 1)
  989. {
  990. if (dump_file)
  991. fprintf (dump_file, ";; Not unrolling, contains branches\n");
  992. return;
  993. }
  994. /* Check whether the loop rolls. */
  995. if ((get_estimated_loop_iterations (loop, &iterations)
  996. || get_max_loop_iterations (loop, &iterations))
  997. && wi::ltu_p (iterations, 2 * nunroll))
  998. {
  999. if (dump_file)
  1000. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  1001. return;
  1002. }
  1003. /* Success. Now force nunroll to be power of 2, as it seems that this
  1004. improves results (partially because of better alignments, partially
  1005. because of some dark magic). */
  1006. for (i = 1; 2 * i <= nunroll; i *= 2)
  1007. continue;
  1008. loop->lpt_decision.decision = LPT_UNROLL_STUPID;
  1009. loop->lpt_decision.times = i - 1;
  1010. }
  1011. /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation does this:
  1012. while (cond)
  1013. body;
  1014. ==> (LOOP->LPT_DECISION.TIMES == 3)
  1015. while (cond)
  1016. {
  1017. body;
  1018. if (!cond) break;
  1019. body;
  1020. if (!cond) break;
  1021. body;
  1022. if (!cond) break;
  1023. body;
  1024. }
  1025. */
  1026. static void
  1027. unroll_loop_stupid (struct loop *loop)
  1028. {
  1029. sbitmap wont_exit;
  1030. unsigned nunroll = loop->lpt_decision.times;
  1031. struct niter_desc *desc = get_simple_loop_desc (loop);
  1032. struct opt_info *opt_info = NULL;
  1033. bool ok;
  1034. if (flag_split_ivs_in_unroller
  1035. || flag_variable_expansion_in_unroller)
  1036. opt_info = analyze_insns_in_loop (loop);
  1037. wont_exit = sbitmap_alloc (nunroll + 1);
  1038. bitmap_clear (wont_exit);
  1039. opt_info_start_duplication (opt_info);
  1040. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  1041. nunroll, wont_exit,
  1042. NULL, NULL,
  1043. DLTHE_FLAG_UPDATE_FREQ
  1044. | (opt_info
  1045. ? DLTHE_RECORD_COPY_NUMBER
  1046. : 0));
  1047. gcc_assert (ok);
  1048. if (opt_info)
  1049. {
  1050. apply_opt_in_copies (opt_info, nunroll, true, true);
  1051. free_opt_info (opt_info);
  1052. }
  1053. free (wont_exit);
  1054. if (desc->simple_p)
  1055. {
  1056. /* We indeed may get here provided that there are nontrivial assumptions
  1057. for a loop to be really simple. We could update the counts, but the
  1058. problem is that we are unable to decide which exit will be taken
  1059. (not really true in case the number of iterations is constant,
  1060. but no one will do anything with this information, so we do not
  1061. worry about it). */
  1062. desc->simple_p = false;
  1063. }
  1064. if (dump_file)
  1065. fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n",
  1066. nunroll, num_loop_insns (loop));
  1067. }
  1068. /* Returns true if REG is referenced in one nondebug insn in LOOP.
  1069. Set *DEBUG_USES to the number of debug insns that reference the
  1070. variable. */
  1071. static bool
  1072. referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg,
  1073. int *debug_uses)
  1074. {
  1075. basic_block *body, bb;
  1076. unsigned i;
  1077. int count_ref = 0;
  1078. rtx_insn *insn;
  1079. body = get_loop_body (loop);
  1080. for (i = 0; i < loop->num_nodes; i++)
  1081. {
  1082. bb = body[i];
  1083. FOR_BB_INSNS (bb, insn)
  1084. if (!rtx_referenced_p (reg, insn))
  1085. continue;
  1086. else if (DEBUG_INSN_P (insn))
  1087. ++*debug_uses;
  1088. else if (++count_ref > 1)
  1089. break;
  1090. }
  1091. free (body);
  1092. return (count_ref == 1);
  1093. }
  1094. /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
  1095. static void
  1096. reset_debug_uses_in_loop (struct loop *loop, rtx reg, int debug_uses)
  1097. {
  1098. basic_block *body, bb;
  1099. unsigned i;
  1100. rtx_insn *insn;
  1101. body = get_loop_body (loop);
  1102. for (i = 0; debug_uses && i < loop->num_nodes; i++)
  1103. {
  1104. bb = body[i];
  1105. FOR_BB_INSNS (bb, insn)
  1106. if (!DEBUG_INSN_P (insn) || !rtx_referenced_p (reg, insn))
  1107. continue;
  1108. else
  1109. {
  1110. validate_change (insn, &INSN_VAR_LOCATION_LOC (insn),
  1111. gen_rtx_UNKNOWN_VAR_LOC (), 0);
  1112. if (!--debug_uses)
  1113. break;
  1114. }
  1115. }
  1116. free (body);
  1117. }
  1118. /* Determine whether INSN contains an accumulator
  1119. which can be expanded into separate copies,
  1120. one for each copy of the LOOP body.
  1121. for (i = 0 ; i < n; i++)
  1122. sum += a[i];
  1123. ==>
  1124. sum += a[i]
  1125. ....
  1126. i = i+1;
  1127. sum1 += a[i]
  1128. ....
  1129. i = i+1
  1130. sum2 += a[i];
  1131. ....
  1132. Return NULL if INSN contains no opportunity for expansion of accumulator.
  1133. Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
  1134. information and return a pointer to it.
  1135. */
  1136. static struct var_to_expand *
  1137. analyze_insn_to_expand_var (struct loop *loop, rtx_insn *insn)
  1138. {
  1139. rtx set, dest, src;
  1140. struct var_to_expand *ves;
  1141. unsigned accum_pos;
  1142. enum rtx_code code;
  1143. int debug_uses = 0;
  1144. set = single_set (insn);
  1145. if (!set)
  1146. return NULL;
  1147. dest = SET_DEST (set);
  1148. src = SET_SRC (set);
  1149. code = GET_CODE (src);
  1150. if (code != PLUS && code != MINUS && code != MULT && code != FMA)
  1151. return NULL;
  1152. if (FLOAT_MODE_P (GET_MODE (dest)))
  1153. {
  1154. if (!flag_associative_math)
  1155. return NULL;
  1156. /* In the case of FMA, we're also changing the rounding. */
  1157. if (code == FMA && !flag_unsafe_math_optimizations)
  1158. return NULL;
  1159. }
  1160. /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
  1161. in MD. But if there is no optab to generate the insn, we can not
  1162. perform the variable expansion. This can happen if an MD provides
  1163. an insn but not a named pattern to generate it, for example to avoid
  1164. producing code that needs additional mode switches like for x87/mmx.
  1165. So we check have_insn_for which looks for an optab for the operation
  1166. in SRC. If it doesn't exist, we can't perform the expansion even
  1167. though INSN is valid. */
  1168. if (!have_insn_for (code, GET_MODE (src)))
  1169. return NULL;
  1170. if (!REG_P (dest)
  1171. && !(GET_CODE (dest) == SUBREG
  1172. && REG_P (SUBREG_REG (dest))))
  1173. return NULL;
  1174. /* Find the accumulator use within the operation. */
  1175. if (code == FMA)
  1176. {
  1177. /* We only support accumulation via FMA in the ADD position. */
  1178. if (!rtx_equal_p (dest, XEXP (src, 2)))
  1179. return NULL;
  1180. accum_pos = 2;
  1181. }
  1182. else if (rtx_equal_p (dest, XEXP (src, 0)))
  1183. accum_pos = 0;
  1184. else if (rtx_equal_p (dest, XEXP (src, 1)))
  1185. {
  1186. /* The method of expansion that we are using; which includes the
  1187. initialization of the expansions with zero and the summation of
  1188. the expansions at the end of the computation will yield wrong
  1189. results for (x = something - x) thus avoid using it in that case. */
  1190. if (code == MINUS)
  1191. return NULL;
  1192. accum_pos = 1;
  1193. }
  1194. else
  1195. return NULL;
  1196. /* It must not otherwise be used. */
  1197. if (code == FMA)
  1198. {
  1199. if (rtx_referenced_p (dest, XEXP (src, 0))
  1200. || rtx_referenced_p (dest, XEXP (src, 1)))
  1201. return NULL;
  1202. }
  1203. else if (rtx_referenced_p (dest, XEXP (src, 1 - accum_pos)))
  1204. return NULL;
  1205. /* It must be used in exactly one insn. */
  1206. if (!referenced_in_one_insn_in_loop_p (loop, dest, &debug_uses))
  1207. return NULL;
  1208. if (dump_file)
  1209. {
  1210. fprintf (dump_file, "\n;; Expanding Accumulator ");
  1211. print_rtl (dump_file, dest);
  1212. fprintf (dump_file, "\n");
  1213. }
  1214. if (debug_uses)
  1215. /* Instead of resetting the debug insns, we could replace each
  1216. debug use in the loop with the sum or product of all expanded
  1217. accummulators. Since we'll only know of all expansions at the
  1218. end, we'd have to keep track of which vars_to_expand a debug
  1219. insn in the loop references, take note of each copy of the
  1220. debug insn during unrolling, and when it's all done, compute
  1221. the sum or product of each variable and adjust the original
  1222. debug insn and each copy thereof. What a pain! */
  1223. reset_debug_uses_in_loop (loop, dest, debug_uses);
  1224. /* Record the accumulator to expand. */
  1225. ves = XNEW (struct var_to_expand);
  1226. ves->insn = insn;
  1227. ves->reg = copy_rtx (dest);
  1228. ves->var_expansions.create (1);
  1229. ves->next = NULL;
  1230. ves->op = GET_CODE (src);
  1231. ves->expansion_count = 0;
  1232. ves->reuse_expansion = 0;
  1233. return ves;
  1234. }
  1235. /* Determine whether there is an induction variable in INSN that
  1236. we would like to split during unrolling.
  1237. I.e. replace
  1238. i = i + 1;
  1239. ...
  1240. i = i + 1;
  1241. ...
  1242. i = i + 1;
  1243. ...
  1244. type chains by
  1245. i0 = i + 1
  1246. ...
  1247. i = i0 + 1
  1248. ...
  1249. i = i0 + 2
  1250. ...
  1251. Return NULL if INSN contains no interesting IVs. Otherwise, allocate
  1252. an IV_TO_SPLIT structure, fill it with the relevant information and return a
  1253. pointer to it. */
  1254. static struct iv_to_split *
  1255. analyze_iv_to_split_insn (rtx_insn *insn)
  1256. {
  1257. rtx set, dest;
  1258. struct rtx_iv iv;
  1259. struct iv_to_split *ivts;
  1260. bool ok;
  1261. /* For now we just split the basic induction variables. Later this may be
  1262. extended for example by selecting also addresses of memory references. */
  1263. set = single_set (insn);
  1264. if (!set)
  1265. return NULL;
  1266. dest = SET_DEST (set);
  1267. if (!REG_P (dest))
  1268. return NULL;
  1269. if (!biv_p (insn, dest))
  1270. return NULL;
  1271. ok = iv_analyze_result (insn, dest, &iv);
  1272. /* This used to be an assert under the assumption that if biv_p returns
  1273. true that iv_analyze_result must also return true. However, that
  1274. assumption is not strictly correct as evidenced by pr25569.
  1275. Returning NULL when iv_analyze_result returns false is safe and
  1276. avoids the problems in pr25569 until the iv_analyze_* routines
  1277. can be fixed, which is apparently hard and time consuming
  1278. according to their author. */
  1279. if (! ok)
  1280. return NULL;
  1281. if (iv.step == const0_rtx
  1282. || iv.mode != iv.extend_mode)
  1283. return NULL;
  1284. /* Record the insn to split. */
  1285. ivts = XNEW (struct iv_to_split);
  1286. ivts->insn = insn;
  1287. ivts->orig_var = dest;
  1288. ivts->base_var = NULL_RTX;
  1289. ivts->step = iv.step;
  1290. ivts->next = NULL;
  1291. return ivts;
  1292. }
  1293. /* Determines which of insns in LOOP can be optimized.
  1294. Return a OPT_INFO struct with the relevant hash tables filled
  1295. with all insns to be optimized. The FIRST_NEW_BLOCK field
  1296. is undefined for the return value. */
  1297. static struct opt_info *
  1298. analyze_insns_in_loop (struct loop *loop)
  1299. {
  1300. basic_block *body, bb;
  1301. unsigned i;
  1302. struct opt_info *opt_info = XCNEW (struct opt_info);
  1303. rtx_insn *insn;
  1304. struct iv_to_split *ivts = NULL;
  1305. struct var_to_expand *ves = NULL;
  1306. iv_to_split **slot1;
  1307. var_to_expand **slot2;
  1308. vec<edge> edges = get_loop_exit_edges (loop);
  1309. edge exit;
  1310. bool can_apply = false;
  1311. iv_analysis_loop_init (loop);
  1312. body = get_loop_body (loop);
  1313. if (flag_split_ivs_in_unroller)
  1314. {
  1315. opt_info->insns_to_split
  1316. = new hash_table<iv_split_hasher> (5 * loop->num_nodes);
  1317. opt_info->iv_to_split_head = NULL;
  1318. opt_info->iv_to_split_tail = &opt_info->iv_to_split_head;
  1319. }
  1320. /* Record the loop exit bb and loop preheader before the unrolling. */
  1321. opt_info->loop_preheader = loop_preheader_edge (loop)->src;
  1322. if (edges.length () == 1)
  1323. {
  1324. exit = edges[0];
  1325. if (!(exit->flags & EDGE_COMPLEX))
  1326. {
  1327. opt_info->loop_exit = split_edge (exit);
  1328. can_apply = true;
  1329. }
  1330. }
  1331. if (flag_variable_expansion_in_unroller
  1332. && can_apply)
  1333. {
  1334. opt_info->insns_with_var_to_expand
  1335. = new hash_table<var_expand_hasher> (5 * loop->num_nodes);
  1336. opt_info->var_to_expand_head = NULL;
  1337. opt_info->var_to_expand_tail = &opt_info->var_to_expand_head;
  1338. }
  1339. for (i = 0; i < loop->num_nodes; i++)
  1340. {
  1341. bb = body[i];
  1342. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
  1343. continue;
  1344. FOR_BB_INSNS (bb, insn)
  1345. {
  1346. if (!INSN_P (insn))
  1347. continue;
  1348. if (opt_info->insns_to_split)
  1349. ivts = analyze_iv_to_split_insn (insn);
  1350. if (ivts)
  1351. {
  1352. slot1 = opt_info->insns_to_split->find_slot (ivts, INSERT);
  1353. gcc_assert (*slot1 == NULL);
  1354. *slot1 = ivts;
  1355. *opt_info->iv_to_split_tail = ivts;
  1356. opt_info->iv_to_split_tail = &ivts->next;
  1357. continue;
  1358. }
  1359. if (opt_info->insns_with_var_to_expand)
  1360. ves = analyze_insn_to_expand_var (loop, insn);
  1361. if (ves)
  1362. {
  1363. slot2 = opt_info->insns_with_var_to_expand->find_slot (ves, INSERT);
  1364. gcc_assert (*slot2 == NULL);
  1365. *slot2 = ves;
  1366. *opt_info->var_to_expand_tail = ves;
  1367. opt_info->var_to_expand_tail = &ves->next;
  1368. }
  1369. }
  1370. }
  1371. edges.release ();
  1372. free (body);
  1373. return opt_info;
  1374. }
  1375. /* Called just before loop duplication. Records start of duplicated area
  1376. to OPT_INFO. */
  1377. static void
  1378. opt_info_start_duplication (struct opt_info *opt_info)
  1379. {
  1380. if (opt_info)
  1381. opt_info->first_new_block = last_basic_block_for_fn (cfun);
  1382. }
  1383. /* Determine the number of iterations between initialization of the base
  1384. variable and the current copy (N_COPY). N_COPIES is the total number
  1385. of newly created copies. UNROLLING is true if we are unrolling
  1386. (not peeling) the loop. */
  1387. static unsigned
  1388. determine_split_iv_delta (unsigned n_copy, unsigned n_copies, bool unrolling)
  1389. {
  1390. if (unrolling)
  1391. {
  1392. /* If we are unrolling, initialization is done in the original loop
  1393. body (number 0). */
  1394. return n_copy;
  1395. }
  1396. else
  1397. {
  1398. /* If we are peeling, the copy in that the initialization occurs has
  1399. number 1. The original loop (number 0) is the last. */
  1400. if (n_copy)
  1401. return n_copy - 1;
  1402. else
  1403. return n_copies;
  1404. }
  1405. }
  1406. /* Allocate basic variable for the induction variable chain. */
  1407. static void
  1408. allocate_basic_variable (struct iv_to_split *ivts)
  1409. {
  1410. rtx expr = SET_SRC (single_set (ivts->insn));
  1411. ivts->base_var = gen_reg_rtx (GET_MODE (expr));
  1412. }
  1413. /* Insert initialization of basic variable of IVTS before INSN, taking
  1414. the initial value from INSN. */
  1415. static void
  1416. insert_base_initialization (struct iv_to_split *ivts, rtx_insn *insn)
  1417. {
  1418. rtx expr = copy_rtx (SET_SRC (single_set (insn)));
  1419. rtx_insn *seq;
  1420. start_sequence ();
  1421. expr = force_operand (expr, ivts->base_var);
  1422. if (expr != ivts->base_var)
  1423. emit_move_insn (ivts->base_var, expr);
  1424. seq = get_insns ();
  1425. end_sequence ();
  1426. emit_insn_before (seq, insn);
  1427. }
  1428. /* Replace the use of induction variable described in IVTS in INSN
  1429. by base variable + DELTA * step. */
  1430. static void
  1431. split_iv (struct iv_to_split *ivts, rtx_insn *insn, unsigned delta)
  1432. {
  1433. rtx expr, *loc, incr, var;
  1434. rtx_insn *seq;
  1435. machine_mode mode = GET_MODE (ivts->base_var);
  1436. rtx src, dest, set;
  1437. /* Construct base + DELTA * step. */
  1438. if (!delta)
  1439. expr = ivts->base_var;
  1440. else
  1441. {
  1442. incr = simplify_gen_binary (MULT, mode,
  1443. ivts->step, gen_int_mode (delta, mode));
  1444. expr = simplify_gen_binary (PLUS, GET_MODE (ivts->base_var),
  1445. ivts->base_var, incr);
  1446. }
  1447. /* Figure out where to do the replacement. */
  1448. loc = &SET_SRC (single_set (insn));
  1449. /* If we can make the replacement right away, we're done. */
  1450. if (validate_change (insn, loc, expr, 0))
  1451. return;
  1452. /* Otherwise, force EXPR into a register and try again. */
  1453. start_sequence ();
  1454. var = gen_reg_rtx (mode);
  1455. expr = force_operand (expr, var);
  1456. if (expr != var)
  1457. emit_move_insn (var, expr);
  1458. seq = get_insns ();
  1459. end_sequence ();
  1460. emit_insn_before (seq, insn);
  1461. if (validate_change (insn, loc, var, 0))
  1462. return;
  1463. /* The last chance. Try recreating the assignment in insn
  1464. completely from scratch. */
  1465. set = single_set (insn);
  1466. gcc_assert (set);
  1467. start_sequence ();
  1468. *loc = var;
  1469. src = copy_rtx (SET_SRC (set));
  1470. dest = copy_rtx (SET_DEST (set));
  1471. src = force_operand (src, dest);
  1472. if (src != dest)
  1473. emit_move_insn (dest, src);
  1474. seq = get_insns ();
  1475. end_sequence ();
  1476. emit_insn_before (seq, insn);
  1477. delete_insn (insn);
  1478. }
  1479. /* Return one expansion of the accumulator recorded in struct VE. */
  1480. static rtx
  1481. get_expansion (struct var_to_expand *ve)
  1482. {
  1483. rtx reg;
  1484. if (ve->reuse_expansion == 0)
  1485. reg = ve->reg;
  1486. else
  1487. reg = ve->var_expansions[ve->reuse_expansion - 1];
  1488. if (ve->var_expansions.length () == (unsigned) ve->reuse_expansion)
  1489. ve->reuse_expansion = 0;
  1490. else
  1491. ve->reuse_expansion++;
  1492. return reg;
  1493. }
  1494. /* Given INSN replace the uses of the accumulator recorded in VE
  1495. with a new register. */
  1496. static void
  1497. expand_var_during_unrolling (struct var_to_expand *ve, rtx_insn *insn)
  1498. {
  1499. rtx new_reg, set;
  1500. bool really_new_expansion = false;
  1501. set = single_set (insn);
  1502. gcc_assert (set);
  1503. /* Generate a new register only if the expansion limit has not been
  1504. reached. Else reuse an already existing expansion. */
  1505. if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS) > ve->expansion_count)
  1506. {
  1507. really_new_expansion = true;
  1508. new_reg = gen_reg_rtx (GET_MODE (ve->reg));
  1509. }
  1510. else
  1511. new_reg = get_expansion (ve);
  1512. validate_replace_rtx_group (SET_DEST (set), new_reg, insn);
  1513. if (apply_change_group ())
  1514. if (really_new_expansion)
  1515. {
  1516. ve->var_expansions.safe_push (new_reg);
  1517. ve->expansion_count++;
  1518. }
  1519. }
  1520. /* Initialize the variable expansions in loop preheader. PLACE is the
  1521. loop-preheader basic block where the initialization of the
  1522. expansions should take place. The expansions are initialized with
  1523. (-0) when the operation is plus or minus to honor sign zero. This
  1524. way we can prevent cases where the sign of the final result is
  1525. effected by the sign of the expansion. Here is an example to
  1526. demonstrate this:
  1527. for (i = 0 ; i < n; i++)
  1528. sum += something;
  1529. ==>
  1530. sum += something
  1531. ....
  1532. i = i+1;
  1533. sum1 += something
  1534. ....
  1535. i = i+1
  1536. sum2 += something;
  1537. ....
  1538. When SUM is initialized with -zero and SOMETHING is also -zero; the
  1539. final result of sum should be -zero thus the expansions sum1 and sum2
  1540. should be initialized with -zero as well (otherwise we will get +zero
  1541. as the final result). */
  1542. static void
  1543. insert_var_expansion_initialization (struct var_to_expand *ve,
  1544. basic_block place)
  1545. {
  1546. rtx_insn *seq;
  1547. rtx var, zero_init;
  1548. unsigned i;
  1549. machine_mode mode = GET_MODE (ve->reg);
  1550. bool honor_signed_zero_p = HONOR_SIGNED_ZEROS (mode);
  1551. if (ve->var_expansions.length () == 0)
  1552. return;
  1553. start_sequence ();
  1554. switch (ve->op)
  1555. {
  1556. case FMA:
  1557. /* Note that we only accumulate FMA via the ADD operand. */
  1558. case PLUS:
  1559. case MINUS:
  1560. FOR_EACH_VEC_ELT (ve->var_expansions, i, var)
  1561. {
  1562. if (honor_signed_zero_p)
  1563. zero_init = simplify_gen_unary (NEG, mode, CONST0_RTX (mode), mode);
  1564. else
  1565. zero_init = CONST0_RTX (mode);
  1566. emit_move_insn (var, zero_init);
  1567. }
  1568. break;
  1569. case MULT:
  1570. FOR_EACH_VEC_ELT (ve->var_expansions, i, var)
  1571. {
  1572. zero_init = CONST1_RTX (GET_MODE (var));
  1573. emit_move_insn (var, zero_init);
  1574. }
  1575. break;
  1576. default:
  1577. gcc_unreachable ();
  1578. }
  1579. seq = get_insns ();
  1580. end_sequence ();
  1581. emit_insn_after (seq, BB_END (place));
  1582. }
  1583. /* Combine the variable expansions at the loop exit. PLACE is the
  1584. loop exit basic block where the summation of the expansions should
  1585. take place. */
  1586. static void
  1587. combine_var_copies_in_loop_exit (struct var_to_expand *ve, basic_block place)
  1588. {
  1589. rtx sum = ve->reg;
  1590. rtx expr, var;
  1591. rtx_insn *seq, *insn;
  1592. unsigned i;
  1593. if (ve->var_expansions.length () == 0)
  1594. return;
  1595. start_sequence ();
  1596. switch (ve->op)
  1597. {
  1598. case FMA:
  1599. /* Note that we only accumulate FMA via the ADD operand. */
  1600. case PLUS:
  1601. case MINUS:
  1602. FOR_EACH_VEC_ELT (ve->var_expansions, i, var)
  1603. sum = simplify_gen_binary (PLUS, GET_MODE (ve->reg), var, sum);
  1604. break;
  1605. case MULT:
  1606. FOR_EACH_VEC_ELT (ve->var_expansions, i, var)
  1607. sum = simplify_gen_binary (MULT, GET_MODE (ve->reg), var, sum);
  1608. break;
  1609. default:
  1610. gcc_unreachable ();
  1611. }
  1612. expr = force_operand (sum, ve->reg);
  1613. if (expr != ve->reg)
  1614. emit_move_insn (ve->reg, expr);
  1615. seq = get_insns ();
  1616. end_sequence ();
  1617. insn = BB_HEAD (place);
  1618. while (!NOTE_INSN_BASIC_BLOCK_P (insn))
  1619. insn = NEXT_INSN (insn);
  1620. emit_insn_after (seq, insn);
  1621. }
  1622. /* Strip away REG_EQUAL notes for IVs we're splitting.
  1623. Updating REG_EQUAL notes for IVs we split is tricky: We
  1624. cannot tell until after unrolling, DF-rescanning, and liveness
  1625. updating, whether an EQ_USE is reached by the split IV while
  1626. the IV reg is still live. See PR55006.
  1627. ??? We cannot use remove_reg_equal_equiv_notes_for_regno,
  1628. because RTL loop-iv requires us to defer rescanning insns and
  1629. any notes attached to them. So resort to old techniques... */
  1630. static void
  1631. maybe_strip_eq_note_for_split_iv (struct opt_info *opt_info, rtx_insn *insn)
  1632. {
  1633. struct iv_to_split *ivts;
  1634. rtx note = find_reg_equal_equiv_note (insn);
  1635. if (! note)
  1636. return;
  1637. for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next)
  1638. if (reg_mentioned_p (ivts->orig_var, note))
  1639. {
  1640. remove_note (insn, note);
  1641. return;
  1642. }
  1643. }
  1644. /* Apply loop optimizations in loop copies using the
  1645. data which gathered during the unrolling. Structure
  1646. OPT_INFO record that data.
  1647. UNROLLING is true if we unrolled (not peeled) the loop.
  1648. REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
  1649. the loop (as it should happen in complete unrolling, but not in ordinary
  1650. peeling of the loop). */
  1651. static void
  1652. apply_opt_in_copies (struct opt_info *opt_info,
  1653. unsigned n_copies, bool unrolling,
  1654. bool rewrite_original_loop)
  1655. {
  1656. unsigned i, delta;
  1657. basic_block bb, orig_bb;
  1658. rtx_insn *insn, *orig_insn, *next;
  1659. struct iv_to_split ivts_templ, *ivts;
  1660. struct var_to_expand ve_templ, *ves;
  1661. /* Sanity check -- we need to put initialization in the original loop
  1662. body. */
  1663. gcc_assert (!unrolling || rewrite_original_loop);
  1664. /* Allocate the basic variables (i0). */
  1665. if (opt_info->insns_to_split)
  1666. for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next)
  1667. allocate_basic_variable (ivts);
  1668. for (i = opt_info->first_new_block;
  1669. i < (unsigned) last_basic_block_for_fn (cfun);
  1670. i++)
  1671. {
  1672. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  1673. orig_bb = get_bb_original (bb);
  1674. /* bb->aux holds position in copy sequence initialized by
  1675. duplicate_loop_to_header_edge. */
  1676. delta = determine_split_iv_delta ((size_t)bb->aux, n_copies,
  1677. unrolling);
  1678. bb->aux = 0;
  1679. orig_insn = BB_HEAD (orig_bb);
  1680. FOR_BB_INSNS_SAFE (bb, insn, next)
  1681. {
  1682. if (!INSN_P (insn)
  1683. || (DEBUG_INSN_P (insn)
  1684. && TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL))
  1685. continue;
  1686. while (!INSN_P (orig_insn)
  1687. || (DEBUG_INSN_P (orig_insn)
  1688. && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn))
  1689. == LABEL_DECL)))
  1690. orig_insn = NEXT_INSN (orig_insn);
  1691. ivts_templ.insn = orig_insn;
  1692. ve_templ.insn = orig_insn;
  1693. /* Apply splitting iv optimization. */
  1694. if (opt_info->insns_to_split)
  1695. {
  1696. maybe_strip_eq_note_for_split_iv (opt_info, insn);
  1697. ivts = opt_info->insns_to_split->find (&ivts_templ);
  1698. if (ivts)
  1699. {
  1700. gcc_assert (GET_CODE (PATTERN (insn))
  1701. == GET_CODE (PATTERN (orig_insn)));
  1702. if (!delta)
  1703. insert_base_initialization (ivts, insn);
  1704. split_iv (ivts, insn, delta);
  1705. }
  1706. }
  1707. /* Apply variable expansion optimization. */
  1708. if (unrolling && opt_info->insns_with_var_to_expand)
  1709. {
  1710. ves = (struct var_to_expand *)
  1711. opt_info->insns_with_var_to_expand->find (&ve_templ);
  1712. if (ves)
  1713. {
  1714. gcc_assert (GET_CODE (PATTERN (insn))
  1715. == GET_CODE (PATTERN (orig_insn)));
  1716. expand_var_during_unrolling (ves, insn);
  1717. }
  1718. }
  1719. orig_insn = NEXT_INSN (orig_insn);
  1720. }
  1721. }
  1722. if (!rewrite_original_loop)
  1723. return;
  1724. /* Initialize the variable expansions in the loop preheader
  1725. and take care of combining them at the loop exit. */
  1726. if (opt_info->insns_with_var_to_expand)
  1727. {
  1728. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1729. insert_var_expansion_initialization (ves, opt_info->loop_preheader);
  1730. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1731. combine_var_copies_in_loop_exit (ves, opt_info->loop_exit);
  1732. }
  1733. /* Rewrite also the original loop body. Find them as originals of the blocks
  1734. in the last copied iteration, i.e. those that have
  1735. get_bb_copy (get_bb_original (bb)) == bb. */
  1736. for (i = opt_info->first_new_block;
  1737. i < (unsigned) last_basic_block_for_fn (cfun);
  1738. i++)
  1739. {
  1740. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  1741. orig_bb = get_bb_original (bb);
  1742. if (get_bb_copy (orig_bb) != bb)
  1743. continue;
  1744. delta = determine_split_iv_delta (0, n_copies, unrolling);
  1745. for (orig_insn = BB_HEAD (orig_bb);
  1746. orig_insn != NEXT_INSN (BB_END (bb));
  1747. orig_insn = next)
  1748. {
  1749. next = NEXT_INSN (orig_insn);
  1750. if (!INSN_P (orig_insn))
  1751. continue;
  1752. ivts_templ.insn = orig_insn;
  1753. if (opt_info->insns_to_split)
  1754. {
  1755. maybe_strip_eq_note_for_split_iv (opt_info, orig_insn);
  1756. ivts = (struct iv_to_split *)
  1757. opt_info->insns_to_split->find (&ivts_templ);
  1758. if (ivts)
  1759. {
  1760. if (!delta)
  1761. insert_base_initialization (ivts, orig_insn);
  1762. split_iv (ivts, orig_insn, delta);
  1763. continue;
  1764. }
  1765. }
  1766. }
  1767. }
  1768. }
  1769. /* Release OPT_INFO. */
  1770. static void
  1771. free_opt_info (struct opt_info *opt_info)
  1772. {
  1773. delete opt_info->insns_to_split;
  1774. opt_info->insns_to_split = NULL;
  1775. if (opt_info->insns_with_var_to_expand)
  1776. {
  1777. struct var_to_expand *ves;
  1778. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1779. ves->var_expansions.release ();
  1780. delete opt_info->insns_with_var_to_expand;
  1781. opt_info->insns_with_var_to_expand = NULL;
  1782. }
  1783. free (opt_info);
  1784. }