cfgloop.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /* Natural loop functions
  2. Copyright (C) 1987-2019 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. #ifndef GCC_CFGLOOP_H
  16. #define GCC_CFGLOOP_H
  17. #include "cfgloopmanip.h"
  18. /* Structure to hold decision about unrolling/peeling. */
  19. enum lpt_dec
  20. {
  21. LPT_NONE,
  22. LPT_UNROLL_CONSTANT,
  23. LPT_UNROLL_RUNTIME,
  24. LPT_UNROLL_STUPID
  25. };
  26. struct GTY (()) lpt_decision {
  27. enum lpt_dec decision;
  28. unsigned times;
  29. };
  30. /* The type of extend applied to an IV. */
  31. enum iv_extend_code
  32. {
  33. IV_SIGN_EXTEND,
  34. IV_ZERO_EXTEND,
  35. IV_UNKNOWN_EXTEND
  36. };
  37. /* The structure describing a bound on number of iterations of a loop. */
  38. struct GTY ((chain_next ("%h.next"))) nb_iter_bound {
  39. /* The statement STMT is executed at most ... */
  40. gimple *stmt;
  41. /* ... BOUND + 1 times (BOUND must be an unsigned constant).
  42. The + 1 is added for the following reasons:
  43. a) 0 would otherwise be unused, while we would need to care more about
  44. overflows (as MAX + 1 is sometimes produced as the estimate on number
  45. of executions of STMT).
  46. b) it is consistent with the result of number_of_iterations_exit. */
  47. widest_int bound;
  48. /* True if the statement will cause the loop to be leaved the (at most)
  49. BOUND + 1-st time it is executed, that is, all the statements after it
  50. are executed at most BOUND times. */
  51. bool is_exit;
  52. /* The next bound in the list. */
  53. struct nb_iter_bound *next;
  54. };
  55. /* Description of the loop exit. */
  56. struct GTY ((for_user)) loop_exit {
  57. /* The exit edge. */
  58. edge e;
  59. /* Previous and next exit in the list of the exits of the loop. */
  60. struct loop_exit *prev;
  61. struct loop_exit *next;
  62. /* Next element in the list of loops from that E exits. */
  63. struct loop_exit *next_e;
  64. };
  65. struct loop_exit_hasher : ggc_ptr_hash<loop_exit>
  66. {
  67. typedef edge compare_type;
  68. static hashval_t hash (loop_exit *);
  69. static bool equal (loop_exit *, edge);
  70. static void remove (loop_exit *);
  71. };
  72. typedef struct loop *loop_p;
  73. /* An integer estimation of the number of iterations. Estimate_state
  74. describes what is the state of the estimation. */
  75. enum loop_estimation
  76. {
  77. /* Estimate was not computed yet. */
  78. EST_NOT_COMPUTED,
  79. /* Estimate is ready. */
  80. EST_AVAILABLE,
  81. EST_LAST
  82. };
  83. /* The structure describing non-overflow control induction variable for
  84. loop's exit edge. */
  85. struct GTY ((chain_next ("%h.next"))) control_iv {
  86. tree base;
  87. tree step;
  88. struct control_iv *next;
  89. };
  90. /* Structure to hold information for each natural loop. */
  91. struct GTY ((chain_next ("%h.next"))) loop {
  92. /* Index into loops array. Note indices will never be reused after loop
  93. is destroyed. */
  94. int num;
  95. /* Number of loop insns. */
  96. unsigned ninsns;
  97. /* Basic block of loop header. */
  98. basic_block header;
  99. /* Basic block of loop latch. */
  100. basic_block latch;
  101. /* For loop unrolling/peeling decision. */
  102. struct lpt_decision lpt_decision;
  103. /* Average number of executed insns per iteration. */
  104. unsigned av_ninsns;
  105. /* Number of blocks contained within the loop. */
  106. unsigned num_nodes;
  107. /* Superloops of the loop, starting with the outermost loop. */
  108. vec<loop_p, va_gc> *superloops;
  109. /* The first inner (child) loop or NULL if innermost loop. */
  110. struct loop *inner;
  111. /* Link to the next (sibling) loop. */
  112. struct loop *next;
  113. /* Auxiliary info specific to a pass. */
  114. PTR GTY ((skip (""))) aux;
  115. /* The number of times the latch of the loop is executed. This can be an
  116. INTEGER_CST, or a symbolic expression representing the number of
  117. iterations like "N - 1", or a COND_EXPR containing the runtime
  118. conditions under which the number of iterations is non zero.
  119. Don't access this field directly: number_of_latch_executions
  120. computes and caches the computed information in this field. */
  121. tree nb_iterations;
  122. /* An integer guaranteed to be greater or equal to nb_iterations. Only
  123. valid if any_upper_bound is true. */
  124. widest_int nb_iterations_upper_bound;
  125. widest_int nb_iterations_likely_upper_bound;
  126. /* An integer giving an estimate on nb_iterations. Unlike
  127. nb_iterations_upper_bound, there is no guarantee that it is at least
  128. nb_iterations. */
  129. widest_int nb_iterations_estimate;
  130. /* If > 0, an integer, where the user asserted that for any
  131. I in [ 0, nb_iterations ) and for any J in
  132. [ I, min ( I + safelen, nb_iterations ) ), the Ith and Jth iterations
  133. of the loop can be safely evaluated concurrently. */
  134. int safelen;
  135. /* Constraints are generally set by consumers and affect certain
  136. semantics of niter analyzer APIs. Currently the APIs affected are
  137. number_of_iterations_exit* functions and their callers. One typical
  138. use case of constraints is to vectorize possibly infinite loop:
  139. 1) Compute niter->assumptions by calling niter analyzer API and
  140. record it as possible condition for loop versioning.
  141. 2) Clear buffered result of niter/scev analyzer.
  142. 3) Set constraint LOOP_C_FINITE assuming the loop is finite.
  143. 4) Analyze data references. Since data reference analysis depends
  144. on niter/scev analyzer, the point is that niter/scev analysis
  145. is done under circumstance of LOOP_C_FINITE constraint.
  146. 5) Version the loop with niter->assumptions computed in step 1).
  147. 6) Vectorize the versioned loop in which niter->assumptions is
  148. checked to be true.
  149. 7) Update constraints in versioned loops so that niter analyzer
  150. in following passes can use it.
  151. Note consumers are usually the loop optimizers and it is consumers'
  152. responsibility to set/clear constraints correctly. Failing to do
  153. that might result in hard to track down bugs in niter/scev consumers. */
  154. unsigned constraints;
  155. /* An integer estimation of the number of iterations. Estimate_state
  156. describes what is the state of the estimation. */
  157. ENUM_BITFIELD(loop_estimation) estimate_state : 8;
  158. unsigned any_upper_bound : 1;
  159. unsigned any_estimate : 1;
  160. unsigned any_likely_upper_bound : 1;
  161. /* True if the loop can be parallel. */
  162. unsigned can_be_parallel : 1;
  163. /* True if -Waggressive-loop-optimizations warned about this loop
  164. already. */
  165. unsigned warned_aggressive_loop_optimizations : 1;
  166. /* True if this loop should never be vectorized. */
  167. unsigned dont_vectorize : 1;
  168. /* True if we should try harder to vectorize this loop. */
  169. unsigned force_vectorize : 1;
  170. /* True if the loop is part of an oacc kernels region. */
  171. unsigned in_oacc_kernels_region : 1;
  172. /* The number of times to unroll the loop. 0 means no information given,
  173. just do what we always do. A value of 1 means do not unroll the loop.
  174. A value of USHRT_MAX means unroll with no specific unrolling factor.
  175. Other values means unroll with the given unrolling factor. */
  176. unsigned short unroll;
  177. /* If this loop was inlined the main clique of the callee which does
  178. not need remapping when copying the loop body. */
  179. unsigned short owned_clique;
  180. /* For SIMD loops, this is a unique identifier of the loop, referenced
  181. by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE
  182. builtins. */
  183. tree simduid;
  184. /* In loop optimization, it's common to generate loops from the original
  185. loop. This field records the index of the original loop which can be
  186. used to track the original loop from newly generated loops. This can
  187. be done by calling function get_loop (cfun, orig_loop_num). Note the
  188. original loop could be destroyed for various reasons thus no longer
  189. exists, as a result, function call to get_loop returns NULL pointer.
  190. In this case, this field should not be used and needs to be cleared
  191. whenever possible. */
  192. int orig_loop_num;
  193. /* Upper bound on number of iterations of a loop. */
  194. struct nb_iter_bound *bounds;
  195. /* Non-overflow control ivs of a loop. */
  196. struct control_iv *control_ivs;
  197. /* Head of the cyclic list of the exits of the loop. */
  198. struct loop_exit *exits;
  199. /* Number of iteration analysis data for RTL. */
  200. struct niter_desc *simple_loop_desc;
  201. /* For sanity checking during loop fixup we record here the former
  202. loop header for loops marked for removal. Note that this prevents
  203. the basic-block from being collected but its index can still be
  204. reused. */
  205. basic_block former_header;
  206. };
  207. /* Set if the loop is known to be infinite. */
  208. #define LOOP_C_INFINITE (1 << 0)
  209. /* Set if the loop is known to be finite without any assumptions. */
  210. #define LOOP_C_FINITE (1 << 1)
  211. /* Set C to the LOOP constraint. */
  212. static inline void
  213. loop_constraint_set (struct loop *loop, unsigned c)
  214. {
  215. loop->constraints |= c;
  216. }
  217. /* Clear C from the LOOP constraint. */
  218. static inline void
  219. loop_constraint_clear (struct loop *loop, unsigned c)
  220. {
  221. loop->constraints &= ~c;
  222. }
  223. /* Check if C is set in the LOOP constraint. */
  224. static inline bool
  225. loop_constraint_set_p (struct loop *loop, unsigned c)
  226. {
  227. return (loop->constraints & c) == c;
  228. }
  229. /* Flags for state of loop structure. */
  230. enum
  231. {
  232. LOOPS_HAVE_PREHEADERS = 1,
  233. LOOPS_HAVE_SIMPLE_LATCHES = 2,
  234. LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
  235. LOOPS_HAVE_RECORDED_EXITS = 8,
  236. LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
  237. LOOP_CLOSED_SSA = 32,
  238. LOOPS_NEED_FIXUP = 64,
  239. LOOPS_HAVE_FALLTHRU_PREHEADERS = 128
  240. };
  241. #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
  242. | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
  243. #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
  244. /* Structure to hold CFG information about natural loops within a function. */
  245. struct GTY (()) loops {
  246. /* State of loops. */
  247. int state;
  248. /* Array of the loops. */
  249. vec<loop_p, va_gc> *larray;
  250. /* Maps edges to the list of their descriptions as loop exits. Edges
  251. whose sources or destinations have loop_father == NULL (which may
  252. happen during the cfg manipulations) should not appear in EXITS. */
  253. hash_table<loop_exit_hasher> *GTY(()) exits;
  254. /* Pointer to root of loop hierarchy tree. */
  255. struct loop *tree_root;
  256. };
  257. /* Loop recognition. */
  258. bool bb_loop_header_p (basic_block);
  259. void init_loops_structure (struct function *, struct loops *, unsigned);
  260. extern struct loops *flow_loops_find (struct loops *);
  261. extern void disambiguate_loops_with_multiple_latches (void);
  262. extern void flow_loops_free (struct loops *);
  263. extern void flow_loops_dump (FILE *,
  264. void (*)(const struct loop *, FILE *, int), int);
  265. extern void flow_loop_dump (const struct loop *, FILE *,
  266. void (*)(const struct loop *, FILE *, int), int);
  267. struct loop *alloc_loop (void);
  268. extern void flow_loop_free (struct loop *);
  269. int flow_loop_nodes_find (basic_block, struct loop *);
  270. unsigned fix_loop_structure (bitmap changed_bbs);
  271. bool mark_irreducible_loops (void);
  272. void release_recorded_exits (function *);
  273. void record_loop_exits (void);
  274. void rescan_loop_exit (edge, bool, bool);
  275. void sort_sibling_loops (function *);
  276. /* Loop data structure manipulation/querying. */
  277. extern void flow_loop_tree_node_add (struct loop *, struct loop *,
  278. struct loop * = NULL);
  279. extern void flow_loop_tree_node_remove (struct loop *);
  280. extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
  281. extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block);
  282. extern struct loop * find_common_loop (struct loop *, struct loop *);
  283. struct loop *superloop_at_depth (struct loop *, unsigned);
  284. struct eni_weights;
  285. extern int num_loop_insns (const struct loop *);
  286. extern int average_num_loop_insns (const struct loop *);
  287. extern unsigned get_loop_level (const struct loop *);
  288. extern bool loop_exit_edge_p (const struct loop *, const_edge);
  289. extern bool loop_exits_to_bb_p (struct loop *, basic_block);
  290. extern bool loop_exits_from_bb_p (struct loop *, basic_block);
  291. extern void mark_loop_exit_edges (void);
  292. extern dump_user_location_t get_loop_location (struct loop *loop);
  293. /* Loops & cfg manipulation. */
  294. extern basic_block *get_loop_body (const struct loop *);
  295. extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
  296. unsigned);
  297. extern basic_block *get_loop_body_in_dom_order (const struct loop *);
  298. extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
  299. extern basic_block *get_loop_body_in_custom_order (const struct loop *,
  300. int (*) (const void *, const void *));
  301. extern vec<edge> get_loop_exit_edges (const struct loop *);
  302. extern edge single_exit (const struct loop *);
  303. extern edge single_likely_exit (struct loop *loop);
  304. extern unsigned num_loop_branches (const struct loop *);
  305. extern edge loop_preheader_edge (const struct loop *);
  306. extern edge loop_latch_edge (const struct loop *);
  307. extern void add_bb_to_loop (basic_block, struct loop *);
  308. extern void remove_bb_from_loops (basic_block);
  309. extern void cancel_loop_tree (struct loop *);
  310. extern void delete_loop (struct loop *);
  311. extern void verify_loop_structure (void);
  312. /* Loop analysis. */
  313. extern bool just_once_each_iteration_p (const struct loop *, const_basic_block);
  314. gcov_type expected_loop_iterations_unbounded (const struct loop *,
  315. bool *read_profile_p = NULL, bool by_profile_only = false);
  316. extern unsigned expected_loop_iterations (struct loop *);
  317. extern rtx doloop_condition_get (rtx_insn *);
  318. void mark_loop_for_removal (loop_p);
  319. /* Induction variable analysis. */
  320. /* The description of induction variable. The things are a bit complicated
  321. due to need to handle subregs and extends. The value of the object described
  322. by it can be obtained as follows (all computations are done in extend_mode):
  323. Value in i-th iteration is
  324. delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
  325. If first_special is true, the value in the first iteration is
  326. delta + mult * base
  327. If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is
  328. subreg_{mode} (base + i * step)
  329. The get_iv_value function can be used to obtain these expressions.
  330. ??? Add a third mode field that would specify the mode in that inner
  331. computation is done, which would enable it to be different from the
  332. outer one? */
  333. struct rtx_iv
  334. {
  335. /* Its base and step (mode of base and step is supposed to be extend_mode,
  336. see the description above). */
  337. rtx base, step;
  338. /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND,
  339. or IV_UNKNOWN_EXTEND). */
  340. enum iv_extend_code extend;
  341. /* Operations applied in the extended mode. */
  342. rtx delta, mult;
  343. /* The mode it is extended to. */
  344. scalar_int_mode extend_mode;
  345. /* The mode the variable iterates in. */
  346. scalar_int_mode mode;
  347. /* Whether the first iteration needs to be handled specially. */
  348. unsigned first_special : 1;
  349. };
  350. /* The description of an exit from the loop and of the number of iterations
  351. till we take the exit. */
  352. struct GTY(()) niter_desc
  353. {
  354. /* The edge out of the loop. */
  355. edge out_edge;
  356. /* The other edge leading from the condition. */
  357. edge in_edge;
  358. /* True if we are able to say anything about number of iterations of the
  359. loop. */
  360. bool simple_p;
  361. /* True if the loop iterates the constant number of times. */
  362. bool const_iter;
  363. /* Number of iterations if constant. */
  364. uint64_t niter;
  365. /* Assumptions under that the rest of the information is valid. */
  366. rtx assumptions;
  367. /* Assumptions under that the loop ends before reaching the latch,
  368. even if value of niter_expr says otherwise. */
  369. rtx noloop_assumptions;
  370. /* Condition under that the loop is infinite. */
  371. rtx infinite;
  372. /* Whether the comparison is signed. */
  373. bool signed_p;
  374. /* The mode in that niter_expr should be computed. */
  375. scalar_int_mode mode;
  376. /* The number of iterations of the loop. */
  377. rtx niter_expr;
  378. };
  379. extern void iv_analysis_loop_init (struct loop *);
  380. extern bool iv_analyze (rtx_insn *, scalar_int_mode, rtx, struct rtx_iv *);
  381. extern bool iv_analyze_result (rtx_insn *, rtx, struct rtx_iv *);
  382. extern bool iv_analyze_expr (rtx_insn *, scalar_int_mode, rtx,
  383. struct rtx_iv *);
  384. extern rtx get_iv_value (struct rtx_iv *, rtx);
  385. extern bool biv_p (rtx_insn *, scalar_int_mode, rtx);
  386. extern void find_simple_exit (struct loop *, struct niter_desc *);
  387. extern void iv_analysis_done (void);
  388. extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
  389. extern void free_simple_loop_desc (struct loop *loop);
  390. static inline struct niter_desc *
  391. simple_loop_desc (struct loop *loop)
  392. {
  393. return loop->simple_loop_desc;
  394. }
  395. /* Accessors for the loop structures. */
  396. /* Returns the loop with index NUM from FNs loop tree. */
  397. static inline struct loop *
  398. get_loop (struct function *fn, unsigned num)
  399. {
  400. return (*loops_for_fn (fn)->larray)[num];
  401. }
  402. /* Returns the number of superloops of LOOP. */
  403. static inline unsigned
  404. loop_depth (const struct loop *loop)
  405. {
  406. return vec_safe_length (loop->superloops);
  407. }
  408. /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost
  409. loop. */
  410. static inline struct loop *
  411. loop_outer (const struct loop *loop)
  412. {
  413. unsigned n = vec_safe_length (loop->superloops);
  414. if (n == 0)
  415. return NULL;
  416. return (*loop->superloops)[n - 1];
  417. }
  418. /* Returns true if LOOP has at least one exit edge. */
  419. static inline bool
  420. loop_has_exit_edges (const struct loop *loop)
  421. {
  422. return loop->exits->next->e != NULL;
  423. }
  424. /* Returns the list of loops in FN. */
  425. inline vec<loop_p, va_gc> *
  426. get_loops (struct function *fn)
  427. {
  428. struct loops *loops = loops_for_fn (fn);
  429. if (!loops)
  430. return NULL;
  431. return loops->larray;
  432. }
  433. /* Returns the number of loops in FN (including the removed
  434. ones and the fake loop that forms the root of the loop tree). */
  435. static inline unsigned
  436. number_of_loops (struct function *fn)
  437. {
  438. struct loops *loops = loops_for_fn (fn);
  439. if (!loops)
  440. return 0;
  441. return vec_safe_length (loops->larray);
  442. }
  443. /* Returns true if state of the loops satisfies all properties
  444. described by FLAGS. */
  445. static inline bool
  446. loops_state_satisfies_p (function *fn, unsigned flags)
  447. {
  448. return (loops_for_fn (fn)->state & flags) == flags;
  449. }
  450. static inline bool
  451. loops_state_satisfies_p (unsigned flags)
  452. {
  453. return loops_state_satisfies_p (cfun, flags);
  454. }
  455. /* Sets FLAGS to the loops state. */
  456. static inline void
  457. loops_state_set (function *fn, unsigned flags)
  458. {
  459. loops_for_fn (fn)->state |= flags;
  460. }
  461. static inline void
  462. loops_state_set (unsigned flags)
  463. {
  464. loops_state_set (cfun, flags);
  465. }
  466. /* Clears FLAGS from the loops state. */
  467. static inline void
  468. loops_state_clear (function *fn, unsigned flags)
  469. {
  470. loops_for_fn (fn)->state &= ~flags;
  471. }
  472. static inline void
  473. loops_state_clear (unsigned flags)
  474. {
  475. if (!current_loops)
  476. return;
  477. loops_state_clear (cfun, flags);
  478. }
  479. /* Check loop structure invariants, if internal consistency checks are
  480. enabled. */
  481. static inline void
  482. checking_verify_loop_structure (void)
  483. {
  484. /* VERIFY_LOOP_STRUCTURE essentially asserts that no loops need fixups.
  485. The loop optimizers should never make changes to the CFG which
  486. require loop fixups. But the low level CFG manipulation code may
  487. set the flag conservatively.
  488. Go ahead and clear the flag here. That avoids the assert inside
  489. VERIFY_LOOP_STRUCTURE, and if there is an inconsistency in the loop
  490. structures VERIFY_LOOP_STRUCTURE will detect it.
  491. This also avoid the compile time cost of excessive fixups. */
  492. loops_state_clear (LOOPS_NEED_FIXUP);
  493. if (flag_checking)
  494. verify_loop_structure ();
  495. }
  496. /* Loop iterators. */
  497. /* Flags for loop iteration. */
  498. enum li_flags
  499. {
  500. LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */
  501. LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order,
  502. starting from innermost ones. */
  503. LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */
  504. };
  505. /* The iterator for loops. */
  506. struct loop_iterator
  507. {
  508. loop_iterator (function *fn, loop_p *loop, unsigned flags);
  509. ~loop_iterator ();
  510. inline loop_p next ();
  511. /* The function we are visiting. */
  512. function *fn;
  513. /* The list of loops to visit. */
  514. vec<int> to_visit;
  515. /* The index of the actual loop. */
  516. unsigned idx;
  517. };
  518. inline loop_p
  519. loop_iterator::next ()
  520. {
  521. int anum;
  522. while (this->to_visit.iterate (this->idx, &anum))
  523. {
  524. this->idx++;
  525. loop_p loop = get_loop (fn, anum);
  526. if (loop)
  527. return loop;
  528. }
  529. return NULL;
  530. }
  531. inline
  532. loop_iterator::loop_iterator (function *fn, loop_p *loop, unsigned flags)
  533. {
  534. struct loop *aloop;
  535. unsigned i;
  536. int mn;
  537. this->idx = 0;
  538. this->fn = fn;
  539. if (!loops_for_fn (fn))
  540. {
  541. this->to_visit.create (0);
  542. *loop = NULL;
  543. return;
  544. }
  545. this->to_visit.create (number_of_loops (fn));
  546. mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
  547. if (flags & LI_ONLY_INNERMOST)
  548. {
  549. for (i = 0; vec_safe_iterate (loops_for_fn (fn)->larray, i, &aloop); i++)
  550. if (aloop != NULL
  551. && aloop->inner == NULL
  552. && aloop->num >= mn)
  553. this->to_visit.quick_push (aloop->num);
  554. }
  555. else if (flags & LI_FROM_INNERMOST)
  556. {
  557. /* Push the loops to LI->TO_VISIT in postorder. */
  558. for (aloop = loops_for_fn (fn)->tree_root;
  559. aloop->inner != NULL;
  560. aloop = aloop->inner)
  561. continue;
  562. while (1)
  563. {
  564. if (aloop->num >= mn)
  565. this->to_visit.quick_push (aloop->num);
  566. if (aloop->next)
  567. {
  568. for (aloop = aloop->next;
  569. aloop->inner != NULL;
  570. aloop = aloop->inner)
  571. continue;
  572. }
  573. else if (!loop_outer (aloop))
  574. break;
  575. else
  576. aloop = loop_outer (aloop);
  577. }
  578. }
  579. else
  580. {
  581. /* Push the loops to LI->TO_VISIT in preorder. */
  582. aloop = loops_for_fn (fn)->tree_root;
  583. while (1)
  584. {
  585. if (aloop->num >= mn)
  586. this->to_visit.quick_push (aloop->num);
  587. if (aloop->inner != NULL)
  588. aloop = aloop->inner;
  589. else
  590. {
  591. while (aloop != NULL && aloop->next == NULL)
  592. aloop = loop_outer (aloop);
  593. if (aloop == NULL)
  594. break;
  595. aloop = aloop->next;
  596. }
  597. }
  598. }
  599. *loop = this->next ();
  600. }
  601. inline
  602. loop_iterator::~loop_iterator ()
  603. {
  604. this->to_visit.release ();
  605. }
  606. #define FOR_EACH_LOOP(LOOP, FLAGS) \
  607. for (loop_iterator li(cfun, &(LOOP), FLAGS); \
  608. (LOOP); \
  609. (LOOP) = li.next ())
  610. #define FOR_EACH_LOOP_FN(FN, LOOP, FLAGS) \
  611. for (loop_iterator li(FN, &(LOOP), FLAGS); \
  612. (LOOP); \
  613. (LOOP) = li.next ())
  614. /* The properties of the target. */
  615. struct target_cfgloop {
  616. /* Number of available registers. */
  617. unsigned x_target_avail_regs;
  618. /* Number of available registers that are call-clobbered. */
  619. unsigned x_target_clobbered_regs;
  620. /* Number of registers reserved for temporary expressions. */
  621. unsigned x_target_res_regs;
  622. /* The cost for register when there still is some reserve, but we are
  623. approaching the number of available registers. */
  624. unsigned x_target_reg_cost[2];
  625. /* The cost for register when we need to spill. */
  626. unsigned x_target_spill_cost[2];
  627. };
  628. extern struct target_cfgloop default_target_cfgloop;
  629. #if SWITCHABLE_TARGET
  630. extern struct target_cfgloop *this_target_cfgloop;
  631. #else
  632. #define this_target_cfgloop (&default_target_cfgloop)
  633. #endif
  634. #define target_avail_regs \
  635. (this_target_cfgloop->x_target_avail_regs)
  636. #define target_clobbered_regs \
  637. (this_target_cfgloop->x_target_clobbered_regs)
  638. #define target_res_regs \
  639. (this_target_cfgloop->x_target_res_regs)
  640. #define target_reg_cost \
  641. (this_target_cfgloop->x_target_reg_cost)
  642. #define target_spill_cost \
  643. (this_target_cfgloop->x_target_spill_cost)
  644. /* Register pressure estimation for induction variable optimizations & loop
  645. invariant motion. */
  646. extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool);
  647. extern void init_set_costs (void);
  648. /* Loop optimizer initialization. */
  649. extern void loop_optimizer_init (unsigned);
  650. extern void loop_optimizer_finalize (function *);
  651. inline void
  652. loop_optimizer_finalize ()
  653. {
  654. loop_optimizer_finalize (cfun);
  655. }
  656. /* Optimization passes. */
  657. enum
  658. {
  659. UAP_UNROLL = 1, /* Enables unrolling of loops if it seems profitable. */
  660. UAP_UNROLL_ALL = 2 /* Enables unrolling of all loops. */
  661. };
  662. extern void doloop_optimize_loops (void);
  663. extern void move_loop_invariants (void);
  664. extern vec<basic_block> get_loop_hot_path (const struct loop *loop);
  665. /* Returns the outermost loop of the loop nest that contains LOOP.*/
  666. static inline struct loop *
  667. loop_outermost (struct loop *loop)
  668. {
  669. unsigned n = vec_safe_length (loop->superloops);
  670. if (n <= 1)
  671. return loop;
  672. return (*loop->superloops)[1];
  673. }
  674. extern void record_niter_bound (struct loop *, const widest_int &, bool, bool);
  675. extern HOST_WIDE_INT get_estimated_loop_iterations_int (struct loop *);
  676. extern HOST_WIDE_INT get_max_loop_iterations_int (const struct loop *);
  677. extern HOST_WIDE_INT get_likely_max_loop_iterations_int (struct loop *);
  678. extern bool get_estimated_loop_iterations (struct loop *loop, widest_int *nit);
  679. extern bool get_max_loop_iterations (const struct loop *loop, widest_int *nit);
  680. extern bool get_likely_max_loop_iterations (struct loop *loop, widest_int *nit);
  681. extern int bb_loop_depth (const_basic_block);
  682. /* Converts VAL to widest_int. */
  683. static inline widest_int
  684. gcov_type_to_wide_int (gcov_type val)
  685. {
  686. HOST_WIDE_INT a[2];
  687. a[0] = (unsigned HOST_WIDE_INT) val;
  688. /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by
  689. the size of type. */
  690. val >>= HOST_BITS_PER_WIDE_INT - 1;
  691. val >>= 1;
  692. a[1] = (unsigned HOST_WIDE_INT) val;
  693. return widest_int::from_array (a, 2);
  694. }
  695. #endif /* GCC_CFGLOOP_H */