[PATCH] rtlopt merge part 1 -- loop analysis (resent)

Zdenek Dvorak rakdver@atrey.karlin.mff.cuni.cz
Fri Jan 24 00:46:00 GMT 2003


Hello,

here is the updated version of patch

http://gcc.gnu.org/ml/gcc-patches/2002-12/msg01029.html

Zdenek

	* cfgloopanal.c: New file.
	* cfgloopmanip.c: New file.
	* Makefile.in (cfgloopanal.o, cfgloopmanip.o): New.
	(toplev.o, loop.o, doloop.o, unroll.o, cfgloop.o, predict.o,
	cfglayout.o): Add dependency on cfgloop.h.
	(cfgloop.o): Add flags.h dependency.
	* basic-block.h (BB_IRREDUCIBLE_LOOP, BB_SUPERBLOCK): New flags.
	(VLS_EXPECT_PREHEADERS, VLS_EXPECT_SIMPLE_LATCHES): Removed.
	(struct loop, struct loops, flow_loops_find, flow_loops_update,
	flow_loops_free, flow_loops_dump, flow_loop_dump,
	flow_loop_scan, flow_loop_tree_node_add, flow_loop_tree_node_remove,
	LOOP_TREE,,LOOP_PRE_HEADER, LOOP_ENTRY_EDGES, LOOP_EXIT_EDGES,
	LOOP_ALL, flow_loop_outside_edge_p, flow_loop_nested_p,
	flow_bb_inside_loop_p, get_loop_body, loop_preheader_edge,
	loop_latch_edge, add_bb_to_loop, remove_bb_from_loops,
	find_common_loop, verify_loop_structure): Declarations moved to ...
	* cfgloop.h: New file.
	* bb-reorder.c (reorder_basic_blocks): Modified.
	* cfglayout.c: Include cfgloop.h.
	(cleanup_unconditional_jumps, cfg_layout_redirect_edge,
	cfg_layout_duplicate_bb, cfg_layout_initialize): Update loop structure.
	(break_superblocks): New static function.
	(cfg_layout_finalize): Use it.
	(cfg_layout_split_block): New function.
	* cfglayout.h (struct reorder_block_def): Add copy and duplicated
	fields.
	(cfg_layout_initialize, cfg_layout_redirect_edge): Declaration
	changed.
	(cfg_layout_split_block): Declare.
	* cfgloop.c: Include cfgloop.h and flags.h.
	(flow_loop_dump, flow_loops_free, flow_loop_exit_edges_find,
	get_loop_body): Avoid signed versus unsigned comparison warnings.
	(make_forwarder_block, flow_loops_find, loop_preheader_edge,
	loop_latch_edge): Modified.
	(verify_loop_structure): Modified to use flags stored in loop structure;
	check irreducible loops.
	(cancel_loop, cancel_loop_tree): New functions.
	(estimate_probability): Use loop analysis code for predictions.
	(estimate_loops_at_level):  Avoid signed versus unsigned comparison
	warnings.
	* doloop.c: Include cfgloop.h.
	* loop.c: Include cfgloop.h.
	* predict.c: Include cfgloop.h.
	* toplev.c: Include cfgloop.h.
	* unroll.c: Include cfgloop.h.
	* tracer.c (tracer): Modified.

Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Makefile.in,v
retrieving revision 1.975
diff -c -3 -p -r1.975 Makefile.in
*** Makefile.in	22 Jan 2003 04:58:26 -0000	1.975
--- Makefile.in	24 Jan 2003 00:28:26 -0000
*************** C_OBJS = c-parse.o c-lang.o c-pretty-pri
*** 770,775 ****
--- 770,776 ----
  
  OBJS = alias.o bb-reorder.o bitmap.o builtins.o caller-save.o calls.o	   \
   cfg.o cfganal.o cfgbuild.o cfgcleanup.o cfglayout.o cfgloop.o		   \
+  cfgloopanal.o cfgloopmanip.o 						   \
   cfgrtl.o combine.o conflict.o convert.o cse.o cselib.o dbxout.o	   \
   debug.o df.o diagnostic.o doloop.o dominance.o		                   \
   dwarf2asm.o dwarf2out.o dwarfout.o emit-rtl.o except.o explow.o	   \
*************** toplev.o : toplev.c $(CONFIG_H) $(SYSTEM
*** 1428,1434 ****
     dwarf2out.h sdbout.h dbxout.h $(EXPR_H) hard-reg-set.h $(BASIC_BLOCK_H) \
     graph.h $(LOOP_H) except.h $(REGS_H) $(TIMEVAR_H) $(lang_options_files) \
     ssa.h $(PARAMS_H) $(TM_P_H) reload.h dwarf2asm.h $(TARGET_H) \
!    langhooks.h insn-flags.h options.h cfglayout.h real.h
  	$(CC) $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
  	  -DTARGET_NAME=\"$(target_alias)\" \
  	  -c $(srcdir)/toplev.c $(OUTPUT_OPTION)
--- 1429,1435 ----
     dwarf2out.h sdbout.h dbxout.h $(EXPR_H) hard-reg-set.h $(BASIC_BLOCK_H) \
     graph.h $(LOOP_H) except.h $(REGS_H) $(TIMEVAR_H) $(lang_options_files) \
     ssa.h $(PARAMS_H) $(TM_P_H) reload.h dwarf2asm.h $(TARGET_H) \
!    langhooks.h insn-flags.h options.h cfglayout.h real.h cfgloop.h
  	$(CC) $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
  	  -DTARGET_NAME=\"$(target_alias)\" \
  	  -c $(srcdir)/toplev.c $(OUTPUT_OPTION)
*************** profile.o : profile.c $(CONFIG_H) $(SYST
*** 1560,1572 ****
     $(TARGET_H) langhooks.h profile.h libfuncs.h gt-profile.h $(HASHTAB_H)
  loop.o : loop.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) flags.h $(LOOP_H) \
     insn-config.h $(REGS_H) hard-reg-set.h $(RECOG_H) $(EXPR_H) \
!    real.h $(PREDICT_H) $(BASIC_BLOCK_H) function.h \
     toplev.h varray.h except.h cselib.h $(OPTABS_H) $(TM_P_H)
  doloop.o : doloop.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) flags.h \
!    $(LOOP_H) $(EXPR_H) hard-reg-set.h $(BASIC_BLOCK_H) $(TM_P_H) toplev.h
  unroll.o : unroll.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) insn-config.h \
     function.h $(INTEGRATE_H) $(REGS_H) $(RECOG_H) flags.h $(EXPR_H) $(LOOP_H) toplev.h \
!    hard-reg-set.h varray.h $(BASIC_BLOCK_H) $(TM_P_H) $(PREDICT_H) $(PARAMS_H)
  alloc-pool.o : alloc-pool.c $(CONFIG_H) $(SYSTEM_H) alloc-pool.h
  flow.o : flow.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     flags.h insn-config.h $(BASIC_BLOCK_H) $(REGS_H) hard-reg-set.h output.h toplev.h \
--- 1561,1575 ----
     $(TARGET_H) langhooks.h profile.h libfuncs.h gt-profile.h $(HASHTAB_H)
  loop.o : loop.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) flags.h $(LOOP_H) \
     insn-config.h $(REGS_H) hard-reg-set.h $(RECOG_H) $(EXPR_H) \
!    real.h $(PREDICT_H) $(BASIC_BLOCK_H) function.h cfgloop.h \
     toplev.h varray.h except.h cselib.h $(OPTABS_H) $(TM_P_H)
  doloop.o : doloop.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) flags.h \
!    $(LOOP_H) $(EXPR_H) hard-reg-set.h $(BASIC_BLOCK_H) $(TM_P_H) toplev.h \
!    cfgloop.h
  unroll.o : unroll.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) insn-config.h \
     function.h $(INTEGRATE_H) $(REGS_H) $(RECOG_H) flags.h $(EXPR_H) $(LOOP_H) toplev.h \
!    hard-reg-set.h varray.h $(BASIC_BLOCK_H) $(TM_P_H) $(PREDICT_H) $(PARAMS_H) \
!    cfgloop.h
  alloc-pool.o : alloc-pool.c $(CONFIG_H) $(SYSTEM_H) alloc-pool.h
  flow.o : flow.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     flags.h insn-config.h $(BASIC_BLOCK_H) $(REGS_H) hard-reg-set.h output.h toplev.h \
*************** cfgbuild.o : cfgbuild.c $(CONFIG_H) $(SY
*** 1585,1592 ****
  cfgcleanup.o : cfgcleanup.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     $(TIMEVAR_H) $(BASIC_BLOCK_H) hard-reg-set.h output.h flags.h $(RECOG_H) toplev.h \
     $(GGC_H) insn-config.h cselib.h $(TARGET_H) $(TM_P_H)
! cfgloop.o : cfgloop.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
!    $(BASIC_BLOCK_H) hard-reg-set.h
  dominance.o : dominance.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     hard-reg-set.h $(BASIC_BLOCK_H) et-forest.h
  et-forest.o : et-forest.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) et-forest.h alloc-pool.h
--- 1588,1599 ----
  cfgcleanup.o : cfgcleanup.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     $(TIMEVAR_H) $(BASIC_BLOCK_H) hard-reg-set.h output.h flags.h $(RECOG_H) toplev.h \
     $(GGC_H) insn-config.h cselib.h $(TARGET_H) $(TM_P_H)
! cfgloop.o : cfgloop.c $(CONFIG_H) $(SYSTEM_H) $(RTL_H) \
!    $(BASIC_BLOCK_H) hard-reg-set.h cfgloop.h flags.h
! cfgloopanal.o : cfgloopanal.c $(CONFIG_H) $(SYSTEM_H) $(RTL_H) \
!    $(BASIC_BLOCK_H) hard-reg-set.h cfgloop.h $(EXPR_H)
! cfgloopmanip.o : cfgloopmanip.c $(CONFIG_H) $(SYSTEM_H) $(RTL_H) \
!    $(BASIC_BLOCK_H) hard-reg-set.h cfgloop.h cfglayout.h output.h
  dominance.o : dominance.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     hard-reg-set.h $(BASIC_BLOCK_H) et-forest.h
  et-forest.o : et-forest.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) et-forest.h alloc-pool.h
*************** reg-stack.o : reg-stack.c $(CONFIG_H) $(
*** 1669,1675 ****
  predict.o: predict.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     flags.h insn-config.h $(BASIC_BLOCK_H) $(REGS_H) hard-reg-set.h output.h toplev.h \
     $(RECOG_H) function.h except.h $(EXPR_H) $(TM_P_H) $(PREDICT_H) real.h \
!    $(PARAMS_H) $(TARGET_H)
  lists.o: lists.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) toplev.h $(RTL_H) $(GGC_H)
  bb-reorder.o : bb-reorder.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     $(TREE_H) flags.h $(BASIC_BLOCK_H) hard-reg-set.h output.h cfglayout.h $(TARGET_H)
--- 1676,1682 ----
  predict.o: predict.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     flags.h insn-config.h $(BASIC_BLOCK_H) $(REGS_H) hard-reg-set.h output.h toplev.h \
     $(RECOG_H) function.h except.h $(EXPR_H) $(TM_P_H) $(PREDICT_H) real.h \
!    $(PARAMS_H) $(TARGET_H) cfgloop.h
  lists.o: lists.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) toplev.h $(RTL_H) $(GGC_H)
  bb-reorder.o : bb-reorder.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
     $(TREE_H) flags.h $(BASIC_BLOCK_H) hard-reg-set.h output.h cfglayout.h $(TARGET_H)
*************** tracer.o : tracer.c $(CONFIG_H) $(SYSTEM
*** 1678,1684 ****
     $(PARAMS_H) profile.h
  cfglayout.o : cfglayout.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     insn-config.h $(BASIC_BLOCK_H) hard-reg-set.h output.h function.h \
!    cfglayout.h
  timevar.o : timevar.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TIMEVAR_H) flags.h \
     intl.h toplev.h
  regrename.o : regrename.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
--- 1685,1691 ----
     $(PARAMS_H) profile.h
  cfglayout.o : cfglayout.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_H) \
     insn-config.h $(BASIC_BLOCK_H) hard-reg-set.h output.h function.h \
!    cfglayout.h cfgloop.h
  timevar.o : timevar.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TIMEVAR_H) flags.h \
     intl.h toplev.h
  regrename.o : regrename.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) \
Index: basic-block.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/basic-block.h,v
retrieving revision 1.167
diff -c -3 -p -r1.167 basic-block.h
*** basic-block.h	18 Jan 2003 22:32:05 -0000	1.167
--- basic-block.h	24 Jan 2003 00:28:26 -0000
*************** typedef struct edge_def {
*** 153,158 ****
--- 153,161 ----
  
  #define EDGE_COMPLEX	(EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH)
  
+ /* Declared in cfgloop.h.  */
+ struct loop;
+ struct loops;
  
  /* A basic block is a sequence of instructions with only entry and
     only one exit.  If any one of the instructions are executed, they
*************** typedef struct basic_block_def {
*** 240,245 ****
--- 243,250 ----
  #define BB_NEW			2
  #define BB_REACHABLE		4
  #define BB_VISITED		8
+ #define BB_IRREDUCIBLE_LOOP	16
+ #define BB_SUPERBLOCK		32
  
  /* Number of basic blocks in the current function.  */
  
*************** extern void tidy_fallthru_edges		PARAMS 
*** 360,365 ****
--- 365,373 ----
  extern void flow_reverse_top_sort_order_compute	PARAMS ((int *));
  extern int flow_depth_first_order_compute	PARAMS ((int *, int *));
  extern void flow_preorder_transversal_compute	PARAMS ((int *));
+ extern int dfs_enumerate_from		PARAMS ((basic_block, int,
+ 						bool (*)(basic_block, void *),
+ 						basic_block *, int, void *));
  extern void dump_edge_info		PARAMS ((FILE *, edge, int));
  extern void clear_edges			PARAMS ((void));
  extern void mark_critical_edges		PARAMS ((void));
*************** extern rtx first_insn_after_basic_block_
*** 369,533 ****
  
  typedef struct dominance_info *dominance_info;
  
- /* Structure to hold information for each natural loop.  */
- struct loop
- {
-   /* Index into loops array.  */
-   int num;
- 
-   /* Basic block of loop header.  */
-   basic_block header;
- 
-   /* Basic block of loop latch.  */
-   basic_block latch;
- 
-   /* Basic block of loop pre-header or NULL if it does not exist.  */
-   basic_block pre_header;
- 
-   /* Array of edges along the pre-header extended basic block trace.
-      The source of the first edge is the root node of pre-header
-      extended basic block, if it exists.  */
-   edge *pre_header_edges;
- 
-   /* Number of edges along the pre_header extended basic block trace.  */
-   int num_pre_header_edges;
- 
-   /* The first block in the loop.  This is not necessarily the same as
-      the loop header.  */
-   basic_block first;
- 
-   /* The last block in the loop.  This is not necessarily the same as
-      the loop latch.  */
-   basic_block last;
- 
-   /* Bitmap of blocks contained within the loop.  */
-   sbitmap nodes;
- 
-   /* Number of blocks contained within the loop.  */
-   int num_nodes;
- 
-   /* Array of edges that enter the loop.  */
-   edge *entry_edges;
- 
-   /* Number of edges that enter the loop.  */
-   int num_entries;
- 
-   /* Array of edges that exit the loop.  */
-   edge *exit_edges;
- 
-   /* Number of edges that exit the loop.  */
-   int num_exits;
- 
-   /* Bitmap of blocks that dominate all exits of the loop.  */
-   sbitmap exits_doms;
- 
-   /* The loop nesting depth.  */
-   int depth;
- 
-   /* Superloops of the loop.  */
-   struct loop **pred;
- 
-   /* The height of the loop (enclosed loop levels) within the loop
-      hierarchy tree.  */
-   int level;
- 
-   /* The outer (parent) loop or NULL if outermost loop.  */
-   struct loop *outer;
- 
-   /* The first inner (child) loop or NULL if innermost loop.  */
-   struct loop *inner;
- 
-   /* Link to the next (sibling) loop.  */
-   struct loop *next;
- 
-   /* Nonzero if the loop is invalid (e.g., contains setjmp.).  */
-   int invalid;
- 
-   /* Auxiliary info specific to a pass.  */
-   void *aux;
- 
-   /* The following are currently used by loop.c but they are likely to
-      disappear as loop.c is converted to use the CFG.  */
- 
-   /* Nonzero if the loop has a NOTE_INSN_LOOP_VTOP.  */
-   rtx vtop;
- 
-   /* Nonzero if the loop has a NOTE_INSN_LOOP_CONT.
-      A continue statement will generate a branch to NEXT_INSN (cont).  */
-   rtx cont;
- 
-   /* The NOTE_INSN_LOOP_BEG.  */
-   rtx start;
- 
-   /* The NOTE_INSN_LOOP_END.  */
-   rtx end;
- 
-   /* For a rotated loop that is entered near the bottom,
-      this is the label at the top.  Otherwise it is zero.  */
-   rtx top;
- 
-   /* Place in the loop where control enters.  */
-   rtx scan_start;
- 
-   /* The position where to sink insns out of the loop.  */
-   rtx sink;
- 
-   /* List of all LABEL_REFs which refer to code labels outside the
-      loop.  Used by routines that need to know all loop exits, such as
-      final_biv_value and final_giv_value.
- 
-      This does not include loop exits due to return instructions.
-      This is because all bivs and givs are pseudos, and hence must be
-      dead after a return, so the presence of a return does not affect
-      any of the optimizations that use this info.  It is simpler to
-      just not include return instructions on this list.  */
-   rtx exit_labels;
- 
-   /* The number of LABEL_REFs on exit_labels for this loop and all
-      loops nested inside it.  */
-   int exit_count;
- };
- 
- 
- /* Structure to hold CFG information about natural loops within a function.  */
- struct loops
- {
-   /* Number of natural loops in the function.  */
-   int num;
- 
-   /* Maxium nested loop level in the function.  */
-   int levels;
- 
-   /* Array of natural loop descriptors (scanning this array in reverse order
-      will find the inner loops before their enclosing outer loops).  */
-   struct loop *array;
- 
-   /* The above array is unused in new loop infrastructure and is kept only for
-      purposes of the old loop optimizer.  Instead we store just pointers to
-      loops here.  */
-   struct loop **parray;
- 
-   /* Pointer to root of loop heirachy tree.  */
-   struct loop *tree_root;
- 
-   /* Information derived from the CFG.  */
-   struct cfg
-   {
-     /* The bitmap vector of dominators or NULL if not computed.  */
-     dominance_info dom;
- 
-     /* The ordering of the basic blocks in a depth first search.  */
-     int *dfs_order;
- 
-     /* The reverse completion ordering of the basic blocks found in a
-        depth first search.  */
-     int *rc_order;
-   } cfg;
- 
-   /* Headers shared by multiple loops that should be merged.  */
-   sbitmap shared_headers;
- };
- 
  /* Structure to group all of the information to process IF-THEN and
     IF-THEN-ELSE blocks for the conditional execution support.  This
     needs to be in a public file in case the IFCVT macros call
--- 377,382 ----
*************** typedef struct ce_if_block
*** 555,573 ****
  
  } ce_if_block_t;
  
- extern int flow_loops_find PARAMS ((struct loops *, int flags));
- extern int flow_loops_update PARAMS ((struct loops *, int flags));
- extern void flow_loops_free PARAMS ((struct loops *));
- extern void flow_loops_dump PARAMS ((const struct loops *, FILE *,
- 				     void (*)(const struct loop *,
- 					      FILE *, int), int));
- extern void flow_loop_dump PARAMS ((const struct loop *, FILE *,
- 				    void (*)(const struct loop *,
- 					     FILE *, int), int));
- extern int flow_loop_scan PARAMS ((struct loops *, struct loop *, int));
- extern void flow_loop_tree_node_add PARAMS ((struct loop *, struct loop *));
- extern void flow_loop_tree_node_remove PARAMS ((struct loop *));
- 
  /* This structure maintains an edge list vector.  */
  struct edge_list
  {
--- 404,409 ----
*************** enum update_life_extent
*** 658,672 ****
  #define CLEANUP_THREADING	64	/* Do jump threading.  */
  #define CLEANUP_NO_INSN_DEL	128	/* Do not try to delete trivially dead
  					   insns.  */
- /* Flags for loop discovery.  */
- 
- #define LOOP_TREE		1	/* Build loop hierarchy tree.  */
- #define LOOP_PRE_HEADER		2	/* Analyse loop pre-header.  */
- #define LOOP_ENTRY_EDGES	4	/* Find entry edges.  */
- #define LOOP_EXIT_EDGES		8	/* Find exit edges.  */
- #define LOOP_EDGES		(LOOP_ENTRY_EDGES | LOOP_EXIT_EDGES)
- #define LOOP_ALL	       15	/* All of the above  */
- 
  extern void life_analysis	PARAMS ((rtx, FILE *, int));
  extern int update_life_info	PARAMS ((sbitmap, enum update_life_extent,
  					 int));
--- 494,499 ----
*************** extern void free_aux_for_edges		PARAMS (
*** 752,777 ****
     debugger, and it is declared extern so we don't get warnings about
     it being unused.  */
  extern void verify_flow_info		PARAMS ((void));
- extern bool flow_loop_outside_edge_p	PARAMS ((const struct loop *, edge));
- extern bool flow_loop_nested_p PARAMS ((const struct loop *,
- 					const struct loop *));
- extern bool flow_bb_inside_loop_p       PARAMS ((const struct loop *,
- 						 const basic_block));
- extern basic_block *get_loop_body       PARAMS ((const struct loop *));
- extern int dfs_enumerate_from           PARAMS ((basic_block, int,
- 				         bool (*)(basic_block, void *),
- 					 basic_block *, int, void *));
- 
- extern edge loop_preheader_edge PARAMS ((struct loop *));
- extern edge loop_latch_edge PARAMS ((struct loop *));
- 
- extern void add_bb_to_loop PARAMS ((basic_block, struct loop *));
- extern void remove_bb_from_loops PARAMS ((basic_block));
- extern struct loop * find_common_loop PARAMS ((struct loop *, struct loop *));
- 
- extern void verify_loop_structure PARAMS ((struct loops *, int));
- #define VLS_EXPECT_PREHEADERS 1
- #define VLS_EXPECT_SIMPLE_LATCHES 2
  
  typedef struct conflict_graph_def *conflict_graph;
  
--- 579,584 ----
Index: bb-reorder.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/bb-reorder.c,v
retrieving revision 1.51
diff -c -3 -p -r1.51 bb-reorder.c
*** bb-reorder.c	16 Dec 2002 18:18:59 -0000	1.51
--- bb-reorder.c	24 Jan 2003 00:28:26 -0000
*************** reorder_basic_blocks ()
*** 268,274 ****
    if ((* targetm.cannot_modify_jumps_p) ())
      return;
  
!   cfg_layout_initialize ();
  
    make_reorder_chain ();
  
--- 268,274 ----
    if ((* targetm.cannot_modify_jumps_p) ())
      return;
  
!   cfg_layout_initialize (NULL);
  
    make_reorder_chain ();
  
Index: cfglayout.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cfglayout.c,v
retrieving revision 1.25
diff -c -3 -p -r1.25 cfglayout.c
*** cfglayout.c	18 Jan 2003 02:26:41 -0000	1.25
--- cfglayout.c	24 Jan 2003 00:28:26 -0000
*************** Software Foundation, 59 Temple Place - S
*** 31,36 ****
--- 31,37 ----
  #include "function.h"
  #include "obstack.h"
  #include "cfglayout.h"
+ #include "cfgloop.h"
  
  /* The contents of the current function definition are allocated
     in this obstack, and all are freed at the end of the function.  */
*************** static void set_block_levels		PARAMS ((t
*** 48,57 ****
  static void change_scope		PARAMS ((rtx, tree, tree));
  
  void verify_insn_chain			PARAMS ((void));
! static void cleanup_unconditional_jumps	PARAMS ((void));
  static void fixup_fallthru_exit_predecessor PARAMS ((void));
  static rtx unlink_insn_chain PARAMS ((rtx, rtx));
  static rtx duplicate_insn_chain PARAMS ((rtx, rtx));
  
  static rtx
  unlink_insn_chain (first, last)
--- 49,59 ----
  static void change_scope		PARAMS ((rtx, tree, tree));
  
  void verify_insn_chain			PARAMS ((void));
! static void cleanup_unconditional_jumps	PARAMS ((struct loops *));
  static void fixup_fallthru_exit_predecessor PARAMS ((void));
  static rtx unlink_insn_chain PARAMS ((rtx, rtx));
  static rtx duplicate_insn_chain PARAMS ((rtx, rtx));
+ static void break_superblocks PARAMS ((void));
  
  static rtx
  unlink_insn_chain (first, last)
*************** verify_insn_chain ()
*** 612,621 ****
  /* Remove any unconditional jumps and forwarder block creating fallthru
     edges instead.  During BB reordering, fallthru edges are not required
     to target next basic block in the linear CFG layout, so the unconditional
!    jumps are not needed.  */
  
  static void
! cleanup_unconditional_jumps ()
  {
    basic_block bb;
  
--- 614,625 ----
  /* Remove any unconditional jumps and forwarder block creating fallthru
     edges instead.  During BB reordering, fallthru edges are not required
     to target next basic block in the linear CFG layout, so the unconditional
!    jumps are not needed.  If LOOPS is not null, also update loop structure &
!    dominators.  */
  
  static void
! cleanup_unconditional_jumps (loops)
!      struct loops *loops;
  {
    basic_block bb;
  
*************** cleanup_unconditional_jumps ()
*** 637,642 ****
--- 641,665 ----
  		fprintf (rtl_dump_file, "Removing forwarder BB %i\n",
  			 bb->index);
  
+ 	      if (loops)
+ 		{
+ 		  /* bb cannot be loop header, as it only has one entry
+ 		     edge.  It could be a loop latch.  */
+ 		  if (bb->loop_father->header == bb)
+ 		    abort ();
+ 
+ 		  if (bb->loop_father->latch == bb)
+ 		    bb->loop_father->latch = bb->pred->src;
+ 
+ 		  if (get_immediate_dominator
+ 		      (loops->cfg.dom, bb->succ->dest) == bb)
+ 		    set_immediate_dominator
+ 		      (loops->cfg.dom, bb->succ->dest, bb->pred->src);
+ 
+ 		  remove_bb_from_loops (bb);
+ 		  delete_from_dominance_info (loops->cfg.dom, bb);
+ 		}
+ 
  	      redirect_edge_succ_nodup (bb->pred, bb->succ->dest);
  	      flow_delete_block (bb);
  	      bb = prev;
*************** duplicate_insn_chain (from, to)
*** 822,834 ****
  }
  
  /* Redirect Edge to DEST.  */
! void
  cfg_layout_redirect_edge (e, dest)
       edge e;
       basic_block dest;
  {
    basic_block src = e->src;
    basic_block old_next_bb = src->next_bb;
  
    /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
       in the case the basic block appears to be in sequence.  Avoid this
--- 845,858 ----
  }
  
  /* Redirect Edge to DEST.  */
! bool
  cfg_layout_redirect_edge (e, dest)
       edge e;
       basic_block dest;
  {
    basic_block src = e->src;
    basic_block old_next_bb = src->next_bb;
+   bool ret;
  
    /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
       in the case the basic block appears to be in sequence.  Avoid this
*************** cfg_layout_redirect_edge (e, dest)
*** 849,857 ****
  	    delete_insn (src->end);
  	}
        redirect_edge_succ_nodup (e, dest);
      }
    else
!     redirect_edge_and_branch (e, dest);
  
    /* We don't want simplejumps in the insn stream during cfglayout.  */
    if (simplejump_p (src->end))
--- 873,883 ----
  	    delete_insn (src->end);
  	}
        redirect_edge_succ_nodup (e, dest);
+ 
+       ret = true;
      }
    else
!     ret = redirect_edge_and_branch (e, dest);
  
    /* We don't want simplejumps in the insn stream during cfglayout.  */
    if (simplejump_p (src->end))
*************** cfg_layout_redirect_edge (e, dest)
*** 861,866 ****
--- 887,908 ----
        src->succ->flags |= EDGE_FALLTHRU;
      }
    src->next_bb = old_next_bb;
+ 
+   return ret;
+ }
+ 
+ /* Same as split_block but update cfg_layout structures.  */
+ edge
+ cfg_layout_split_block (bb, insn)
+      basic_block bb;
+      rtx insn;
+ {
+   edge fallthru = split_block (bb, insn);
+ 
+   alloc_aux_for_block (fallthru->dest, sizeof (struct reorder_block_def));
+   RBI (fallthru->dest)->footer = RBI (fallthru->src)->footer;
+   RBI (fallthru->src)->footer = NULL;
+   return fallthru;
  }
  
  /* Create a duplicate of the basic block BB and redirect edge E into it.  */
*************** cfg_layout_duplicate_bb (bb, e)
*** 949,954 ****
--- 991,997 ----
      bb->frequency = 0;
  
    RBI (new_bb)->original = bb;
+   RBI (bb)->copy = new_bb;
    return new_bb;
  }
  
*************** cfg_layout_duplicate_bb (bb, e)
*** 956,972 ****
     CFG layout changes.  It keeps LOOPS up-to-date if not null.  */
  
  void
! cfg_layout_initialize ()
  {
    /* Our algorithm depends on fact that there are now dead jumptables
       around the code.  */
    alloc_aux_for_blocks (sizeof (struct reorder_block_def));
  
!   cleanup_unconditional_jumps ();
  
    record_effective_endpoints ();
  }
  
  /* Finalize the changes: reorder insn list according to the sequence, enter
     compensation code, rebuild scope forest.  */
  
--- 999,1045 ----
     CFG layout changes.  It keeps LOOPS up-to-date if not null.  */
  
  void
! cfg_layout_initialize (loops)
!      struct loops *loops;
  {
    /* Our algorithm depends on fact that there are now dead jumptables
       around the code.  */
    alloc_aux_for_blocks (sizeof (struct reorder_block_def));
  
!   cleanup_unconditional_jumps (loops);
  
    record_effective_endpoints ();
  }
  
+ /* Splits superblocks.  */
+ static void
+ break_superblocks ()
+ {
+   sbitmap superblocks;
+   int i, need;
+ 
+   superblocks = sbitmap_alloc (n_basic_blocks);
+   sbitmap_zero (superblocks);
+ 
+   need = 0;
+ 
+   for (i = 0; i < n_basic_blocks; i++)
+     if (BASIC_BLOCK(i)->flags & BB_SUPERBLOCK)
+       {
+ 	BASIC_BLOCK(i)->flags &= ~BB_SUPERBLOCK;
+ 	SET_BIT (superblocks, i);
+ 	need = 1;
+       }
+ 
+   if (need)
+     {
+       rebuild_jump_labels (get_insns ());
+       find_many_sub_basic_blocks (superblocks);
+     }
+ 
+   free (superblocks);
+ }
+ 
  /* Finalize the changes: reorder insn list according to the sequence, enter
     compensation code, rebuild scope forest.  */
  
*************** cfg_layout_finalize ()
*** 981,986 ****
--- 1054,1061 ----
  #endif
  
    free_aux_for_blocks ();
+ 
+   break_superblocks ();
  
  #ifdef ENABLE_CHECKING
    verify_flow_info ();
Index: cfglayout.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cfglayout.h,v
retrieving revision 1.3
diff -c -3 -p -r1.3 cfglayout.h
*** cfglayout.h	8 May 2002 09:17:17 -0000	1.3
--- cfglayout.h	24 Jan 2003 00:28:26 -0000
*************** typedef struct reorder_block_def
*** 25,30 ****
--- 25,33 ----
    rtx footer;
    basic_block next;
    basic_block original;
+   /* Used by loop copying.  */
+   basic_block copy;
+   int duplicated;
  
    /* These fields are used by bb-reorder pass.  */
    int visited;
*************** typedef struct reorder_block_def
*** 32,41 ****
  
  #define RBI(BB)	((reorder_block_def) (BB)->aux)
  
! extern void cfg_layout_initialize	PARAMS ((void));
  extern void cfg_layout_finalize		PARAMS ((void));
  extern bool cfg_layout_can_duplicate_bb_p PARAMS ((basic_block));
  extern basic_block cfg_layout_duplicate_bb PARAMS ((basic_block, edge));
  extern void scope_to_insns_initialize	PARAMS ((void));
  extern void scope_to_insns_finalize	PARAMS ((void));
! extern void cfg_layout_redirect_edge	PARAMS ((edge, basic_block));
--- 35,45 ----
  
  #define RBI(BB)	((reorder_block_def) (BB)->aux)
  
! extern void cfg_layout_initialize	PARAMS ((struct loops *));
  extern void cfg_layout_finalize		PARAMS ((void));
  extern bool cfg_layout_can_duplicate_bb_p PARAMS ((basic_block));
  extern basic_block cfg_layout_duplicate_bb PARAMS ((basic_block, edge));
  extern void scope_to_insns_initialize	PARAMS ((void));
  extern void scope_to_insns_finalize	PARAMS ((void));
! extern bool cfg_layout_redirect_edge	PARAMS ((edge, basic_block));
! extern edge cfg_layout_split_block	PARAMS ((basic_block, rtx));
Index: cfgloop.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cfgloop.c,v
retrieving revision 1.19
diff -c -3 -p -r1.19 cfgloop.c
*** cfgloop.c	16 Jan 2003 15:49:20 -0000	1.19
--- cfgloop.c	24 Jan 2003 00:28:26 -0000
*************** Software Foundation, 59 Temple Place - S
*** 26,31 ****
--- 26,33 ----
  #include "hard-reg-set.h"
  #include "basic-block.h"
  #include "toplev.h"
+ #include "cfgloop.h"
+ #include "flags.h"
  
  /* Ratio of frequencies of edges so that one of more latch edges is
     considered to belong to inner loop with same header.  */
*************** flow_loop_dump (loop, file, loop_dump_au
*** 114,120 ****
       int verbose;
  {
    basic_block *bbs;
!   int i;
  
    if (! loop || ! loop->header)
      return;
--- 116,122 ----
       int verbose;
  {
    basic_block *bbs;
!   unsigned i;
  
    if (! loop || ! loop->header)
      return;
*************** flow_loops_free (loops)
*** 206,212 ****
  {
    if (loops->parray)
      {
!       int i;
  
        if (! loops->num)
  	abort ();
--- 208,214 ----
  {
    if (loops->parray)
      {
!       unsigned i;
  
        if (! loops->num)
  	abort ();
*************** flow_loop_exit_edges_find (loop)
*** 275,281 ****
  {
    edge e;
    basic_block node, *bbs;
!   int num_exits, i;
  
    loop->exit_edges = NULL;
    loop->num_exits = 0;
--- 277,283 ----
  {
    edge e;
    basic_block node, *bbs;
!   unsigned num_exits, i;
  
    loop->exit_edges = NULL;
    loop->num_exits = 0;
*************** make_forwarder_block (bb, redirect_latch
*** 609,614 ****
--- 611,620 ----
  
    insn = PREV_INSN (first_insn_after_basic_block_note (bb));
  
+   /* For empty block split_block will return NULL.  */
+   if (bb->end == insn)
+     emit_note_after (NOTE_INSN_DELETED, insn);
+ 
    fallthru = split_block (bb, insn);
    dummy = fallthru->src;
    bb = fallthru->dest;
*************** flow_loops_find (loops, flags)
*** 926,934 ****
        loops->cfg.dom = NULL;
        free_dominance_info (dom);
      }
  #ifdef ENABLE_CHECKING
    verify_flow_info ();
!   verify_loop_structure (loops, 0);
  #endif
  
    return loops->num;
--- 932,942 ----
        loops->cfg.dom = NULL;
        free_dominance_info (dom);
      }
+ 
+   loops->state = 0;
  #ifdef ENABLE_CHECKING
    verify_flow_info ();
!   verify_loop_structure (loops);
  #endif
  
    return loops->num;
*************** get_loop_body (loop)
*** 992,998 ****
       const struct loop *loop;
  {
    basic_block *tovisit, bb;
!   int tv = 0;
  
    if (!loop->num_nodes)
      abort ();
--- 1000,1006 ----
       const struct loop *loop;
  {
    basic_block *tovisit, bb;
!   unsigned tv = 0;
  
    if (!loop->num_nodes)
      abort ();
*************** get_loop_body (loop)
*** 1003,1009 ****
    if (loop->latch == EXIT_BLOCK_PTR)
      {
        /* There may be blocks unreachable from EXIT_BLOCK.  */
!       if (loop->num_nodes != n_basic_blocks + 2)
  	abort ();
        FOR_EACH_BB (bb)
  	tovisit[tv++] = bb;
--- 1011,1017 ----
    if (loop->latch == EXIT_BLOCK_PTR)
      {
        /* There may be blocks unreachable from EXIT_BLOCK.  */
!       if (loop->num_nodes != (unsigned) n_basic_blocks + 2)
  	abort ();
        FOR_EACH_BB (bb)
  	tovisit[tv++] = bb;
*************** find_common_loop (loop_s, loop_d)
*** 1073,1090 ****
    return loop_s;
  }
  
  /* Checks that LOOPS are allright:
       -- sizes of loops are allright
       -- results of get_loop_body really belong to the loop
       -- loop header have just single entry edge and single latch edge
       -- loop latches have only single successor that is header of their loop
    */
  void
! verify_loop_structure (loops, flags)
       struct loops *loops;
-      int flags;
  {
!   int *sizes, i, j;
    basic_block *bbs, bb;
    struct loop *loop;
    int err = 0;
--- 1081,1137 ----
    return loop_s;
  }
  
+ /* Cancels the LOOP; it must be innermost one.  */
+ void
+ cancel_loop (loops, loop)
+      struct loops *loops;
+      struct loop *loop;
+ {
+   basic_block *bbs;
+   unsigned i;
+ 
+   if (loop->inner)
+     abort ();
+ 
+   /* Move blocks up one level (they should be removed as soon as possible).  */
+   bbs = get_loop_body (loop);
+   for (i = 0; i < loop->num_nodes; i++)
+     bbs[i]->loop_father = loop->outer;
+ 
+   /* Remove the loop from structure.  */
+   flow_loop_tree_node_remove (loop);
+ 
+   /* Remove loop from loops array.  */
+   loops->parray[loop->num] = NULL;
+ 
+   /* Free loop data.  */
+   flow_loop_free (loop);
+ }
+ 
+ /* Cancels LOOP and all its subloops.  */
+ void
+ cancel_loop_tree (loops, loop)
+      struct loops *loops;
+      struct loop *loop;
+ {
+   while (loop->inner)
+     cancel_loop_tree (loops, loop->inner);
+   cancel_loop (loops, loop);
+ }
+ 
  /* Checks that LOOPS are allright:
       -- sizes of loops are allright
       -- results of get_loop_body really belong to the loop
       -- loop header have just single entry edge and single latch edge
       -- loop latches have only single successor that is header of their loop
+      -- irreducible loops are correctly marked
    */
  void
! verify_loop_structure (loops)
       struct loops *loops;
  {
!   unsigned *sizes, i, j;
!   sbitmap irreds;
    basic_block *bbs, bb;
    struct loop *loop;
    int err = 0;
*************** verify_loop_structure (loops, flags)
*** 1137,1150 ****
        if (!loop)
  	continue;
  
!       if ((flags & VLS_EXPECT_PREHEADERS)
  	  && (!loop->header->pred->pred_next
  	      || loop->header->pred->pred_next->pred_next))
  	{
  	  error ("Loop %d's header does not have exactly 2 entries.", i);
  	  err = 1;
  	}
!       if (flags & VLS_EXPECT_SIMPLE_LATCHES)
  	{
  	  if (!loop->latch->succ
  	      || loop->latch->succ->succ_next)
--- 1184,1197 ----
        if (!loop)
  	continue;
  
!       if ((loops->state & LOOPS_HAVE_PREHEADERS)
  	  && (!loop->header->pred->pred_next
  	      || loop->header->pred->pred_next->pred_next))
  	{
  	  error ("Loop %d's header does not have exactly 2 entries.", i);
  	  err = 1;
  	}
!       if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
  	{
  	  if (!loop->latch->succ
  	      || loop->latch->succ->succ_next)
*************** verify_loop_structure (loops, flags)
*** 1170,1175 ****
--- 1217,1255 ----
  	}
      }
  
+   /* Check irreducible loops.  */
+   if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
+     {
+       /* Record old info.  */
+       irreds = sbitmap_alloc (last_basic_block);
+       FOR_EACH_BB (bb)
+ 	if (bb->flags & BB_IRREDUCIBLE_LOOP)
+ 	  SET_BIT (irreds, bb->index);
+ 	else
+ 	  RESET_BIT (irreds, bb->index);
+ 
+       /* Recount it.  */
+       mark_irreducible_loops (loops);
+ 
+       /* Compare.  */
+       FOR_EACH_BB (bb)
+ 	{
+ 	  if ((bb->flags & BB_IRREDUCIBLE_LOOP)
+ 	      && !TEST_BIT (irreds, bb->index))
+ 	    {
+ 	      error ("Basic block %d should be marked irreducible.", bb->index);
+ 	      err = 1;
+ 	    }
+ 	  else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
+ 	      && TEST_BIT (irreds, bb->index))
+ 	    {
+ 	      error ("Basic block %d should not be marked irreducible.", bb->index);
+ 	      err = 1;
+ 	    }
+ 	}
+       free (irreds);
+     }
+ 
    if (err)
      abort ();
  }
*************** verify_loop_structure (loops, flags)
*** 1177,1183 ****
  /* Returns latch edge of LOOP.  */
  edge
  loop_latch_edge (loop)
!      struct loop *loop;
  {
    edge e;
  
--- 1257,1263 ----
  /* Returns latch edge of LOOP.  */
  edge
  loop_latch_edge (loop)
!      const struct loop *loop;
  {
    edge e;
  
*************** loop_latch_edge (loop)
*** 1190,1196 ****
  /* Returns preheader edge of LOOP.  */
  edge
  loop_preheader_edge (loop)
!      struct loop *loop;
  {
    edge e;
  
--- 1270,1276 ----
  /* Returns preheader edge of LOOP.  */
  edge
  loop_preheader_edge (loop)
!      const struct loop *loop;
  {
    edge e;
  
Index: doloop.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doloop.c,v
retrieving revision 1.27
diff -c -3 -p -r1.27 doloop.c
*** doloop.c	16 Dec 2002 18:19:21 -0000	1.27
--- doloop.c	24 Jan 2003 00:28:26 -0000
*************** Software Foundation, 59 Temple Place - S
*** 31,36 ****
--- 31,37 ----
  #include "basic-block.h"
  #include "toplev.h"
  #include "tm_p.h"
+ #include "cfgloop.h"
  
  
  /* This module is used to modify loops with a determinable number of
Index: flow.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flow.c,v
retrieving revision 1.547
diff -c -3 -p -r1.547 flow.c
*** flow.c	17 Jan 2003 14:24:20 -0000	1.547
--- flow.c	24 Jan 2003 00:28:26 -0000
*************** init_propagate_block_info (bb, live, loc
*** 2000,2012 ****
  	    rtx mem = SET_DEST (set);
  	    rtx canon_mem = canon_rtx (mem);
  
- 	    /* This optimization is performed by faking a store to the
- 	       memory at the end of the block.  This doesn't work for
- 	       unchanging memories because multiple stores to unchanging
- 	       memory is illegal and alias analysis doesn't consider it.  */
- 	    if (RTX_UNCHANGING_P (canon_mem))
- 	      continue;
- 
  	    if (XEXP (canon_mem, 0) == frame_pointer_rtx
  		|| (GET_CODE (XEXP (canon_mem, 0)) == PLUS
  		    && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
--- 2000,2005 ----
*************** insn_dead_p (pbi, x, call_ok, notes)
*** 2181,2189 ****
  	     If so, this memory write is dead (remember, we're walking
  	     backwards from the end of the block to the start).  Since
  	     rtx_equal_p does not check the alias set or flags, we also
! 	     must have the potential for them to conflict (anti_dependence).  */
  	  for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
! 	    if (anti_dependence (r, XEXP (temp, 0)))
  	      {
  		rtx mem = XEXP (temp, 0);
  
--- 2174,2182 ----
  	     If so, this memory write is dead (remember, we're walking
  	     backwards from the end of the block to the start).  Since
  	     rtx_equal_p does not check the alias set or flags, we also
! 	     must have the potential for them to conflict (output_dependence).  */
  	  for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
! 	    if (output_dependence (r, XEXP (temp, 0)))
  	      {
  		rtx mem = XEXP (temp, 0);
  
*************** mark_used_regs (pbi, x, cond, insn)
*** 3801,3808 ****
  
  	      while (temp)
  		{
  		  next = XEXP (temp, 1);
! 		  if (anti_dependence (XEXP (temp, 0), x))
  		    {
  		      /* Splice temp out of the list.  */
  		      if (prev)
--- 3794,3803 ----
  
  	      while (temp)
  		{
+ 		  rtx mem = XEXP (temp, 0);
  		  next = XEXP (temp, 1);
! 		  if (true_dependence (mem, GET_MODE (mem), x,
! 				       rtx_addr_varies_p))
  		    {
  		      /* Splice temp out of the list.  */
  		      if (prev)
Index: loop.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/loop.c,v
retrieving revision 1.438
diff -c -3 -p -r1.438 loop.c
*** loop.c	18 Jan 2003 22:32:06 -0000	1.438
--- loop.c	24 Jan 2003 00:28:26 -0000
*************** Software Foundation, 59 Temple Place - S
*** 56,61 ****
--- 56,62 ----
  #include "predict.h"
  #include "insn-flags.h"
  #include "optabs.h"
+ #include "cfgloop.h"
  
  /* Not really meaningful values, but at least something.  */
  #ifndef SIMULTANEOUS_PREFETCHES
Index: predict.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/predict.c,v
retrieving revision 1.79
diff -c -3 -p -r1.79 predict.c
*** predict.c	17 Jan 2003 03:28:08 -0000	1.79
--- predict.c	24 Jan 2003 00:28:26 -0000
*************** Software Foundation, 59 Temple Place - S
*** 52,57 ****
--- 52,58 ----
  #include "params.h"
  #include "target.h"
  #include "loop.h"
+ #include "cfgloop.h"
  
  /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
  		   1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX.  */
*************** estimate_probability (loops_info)
*** 425,431 ****
  {
    dominance_info dominators, post_dominators;
    basic_block bb;
!   int i;
  
    connect_infinite_loops_to_exit ();
    dominators = calculate_dominance_info (CDI_DOMINATORS);
--- 426,432 ----
  {
    dominance_info dominators, post_dominators;
    basic_block bb;
!   unsigned i;
  
    connect_infinite_loops_to_exit ();
    dominators = calculate_dominance_info (CDI_DOMINATORS);
*************** estimate_probability (loops_info)
*** 436,448 ****
    for (i = 1; i < loops_info->num; i++)
      {
        basic_block bb, *bbs;
!       int j;
        int exits;
        struct loop *loop = loops_info->parray[i];
  
        flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
        exits = loop->num_exits;
  
        bbs = get_loop_body (loop);
        for (j = 0; j < loop->num_nodes; j++)
  	{
--- 437,464 ----
    for (i = 1; i < loops_info->num; i++)
      {
        basic_block bb, *bbs;
!       unsigned j;
        int exits;
        struct loop *loop = loops_info->parray[i];
+       struct loop_desc desc;
+       unsigned HOST_WIDE_INT niter;
  
        flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
        exits = loop->num_exits;
  
+       if (simple_loop_p (loops_info, loop, &desc)
+ 	  && desc.const_iter)
+ 	{
+ 	  niter = desc.niter + 1;
+ 	  if (niter == 0)        /* We might overflow here.  */
+ 	    niter = desc.niter;
+ 
+ 	  predict_edge (desc.in_edge, PRED_LOOP_ITERATIONS,
+ 			REG_BR_PROB_BASE
+ 			- (REG_BR_PROB_BASE + niter /2)
+ 			/ niter);
+ 	}
+ 
        bbs = get_loop_body (loop);
        for (j = 0; j < loop->num_nodes; j++)
  	{
*************** estimate_loops_at_level (first_loop)
*** 1060,1066 ****
      {
        edge e;
        basic_block *bbs;
!       int i;
  
        estimate_loops_at_level (loop->inner);
        
--- 1076,1082 ----
      {
        edge e;
        basic_block *bbs;
!       unsigned i;
  
        estimate_loops_at_level (loop->inner);
        
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.698
diff -c -3 -p -r1.698 toplev.c
*** toplev.c	23 Jan 2003 02:57:26 -0000	1.698
--- toplev.c	24 Jan 2003 00:28:27 -0000
*************** Software Foundation, 59 Temple Place - S
*** 73,78 ****
--- 73,79 ----
  #include "target.h"
  #include "langhooks.h"
  #include "cfglayout.h"
+ #include "cfgloop.h"
  
  #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
  #include "dwarf2out.h"
Index: tracer.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tracer.c,v
retrieving revision 1.6
diff -c -3 -p -r1.6 tracer.c
*** tracer.c	24 Dec 2002 08:30:33 -0000	1.6
--- tracer.c	24 Jan 2003 00:28:27 -0000
*************** tracer ()
*** 366,372 ****
  {
    if (n_basic_blocks <= 1)
      return;
!   cfg_layout_initialize ();
    mark_dfs_back_edges ();
    if (rtl_dump_file)
      dump_flow_info (rtl_dump_file);
--- 366,372 ----
  {
    if (n_basic_blocks <= 1)
      return;
!   cfg_layout_initialize (NULL);
    mark_dfs_back_edges ();
    if (rtl_dump_file)
      dump_flow_info (rtl_dump_file);
Index: unroll.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/unroll.c,v
retrieving revision 1.186
diff -c -3 -p -r1.186 unroll.c
*** unroll.c	24 Dec 2002 08:30:33 -0000	1.186
--- unroll.c	24 Jan 2003 00:28:27 -0000
*************** Software Foundation, 59 Temple Place - S
*** 150,155 ****
--- 150,156 ----
  #include "basic-block.h"
  #include "predict.h"
  #include "params.h"
+ #include "cfgloop.h"
  
  /* The prime factors looked for when trying to unroll a loop by some
     number which is modulo the total number of iterations.  Just checking


cfgloop.h:
----------
/* Natural loop functions
   Copyright (C) 2002, 2003
   Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

/* Structure to hold decision about unrolling/peeling.  */
enum lpt_dec
{
  LPT_NONE,
  LPT_PEEL_COMPLETELY,
  LPT_PEEL_SIMPLE,
  LPT_UNROLL_CONSTANT,
  LPT_UNROLL_RUNTIME,
  LPT_UNROLL_STUPID
};

struct lpt_decision
{
  enum lpt_dec decision;
  unsigned times;
};

/* Description of loop for simple loop unrolling.  */
struct loop_desc
{
  int postincr;		/* 1 if increment/decrement is done after loop exit condition.  */
  rtx stride;		/* Value added to VAR in each iteration.  */
  rtx var;		/* Loop control variable.  */
  rtx var_alts;		/* List of definitions of its initial value.  */
  rtx lim;		/* Expression var is compared with.  */
  rtx lim_alts;		/* List of definitions of its initial value.  */
  bool const_iter;      /* True if it iterates constant number of times.  */
  unsigned HOST_WIDE_INT niter;
			/* Number of iterations if it is constant.  */
  bool may_be_zero;     /* If we cannot determine that the first iteration will pass.  */
  enum rtx_code cond;	/* Exit condition.  */
  int neg;		/* Set to 1 if loop ends when condition is satisfied.  */
  edge out_edge;	/* The exit edge.  */
  edge in_edge;		/* And the other one.  */
  int n_branches;	/* Number of branches inside the loop.  */
};

/* Structure to hold information for each natural loop.  */
struct loop
{
  /* Index into loops array.  */
  int num;

  /* Basic block of loop header.  */
  basic_block header;

  /* Basic block of loop latch.  */
  basic_block latch;

  /* Basic block of loop preheader or NULL if it does not exist.  */
  basic_block pre_header;

  /* For loop unrolling/peeling decision.  */
  struct lpt_decision lpt_decision;

  /* Simple loop description.  */
  int simple;
  struct loop_desc desc;
  int has_desc;

  /* Number of loop insns.  */
  unsigned ninsns;

  /* Average number of executed insns per iteration.  */
  unsigned av_ninsns;

  /* Array of edges along the preheader extended basic block trace.
     The source of the first edge is the root node of preheader
     extended basic block, if it exists.  */
  edge *pre_header_edges;

  /* Number of edges along the pre_header extended basic block trace.  */
  int num_pre_header_edges;

  /* The first block in the loop.  This is not necessarily the same as
     the loop header.  */
  basic_block first;

  /* The last block in the loop.  This is not necessarily the same as
     the loop latch.  */
  basic_block last;

  /* Bitmap of blocks contained within the loop.  */
  sbitmap nodes;

  /* Number of blocks contained within the loop.  */
  unsigned num_nodes;

  /* Array of edges that enter the loop.  */
  edge *entry_edges;

  /* Number of edges that enter the loop.  */
  int num_entries;

  /* Array of edges that exit the loop.  */
  edge *exit_edges;

  /* Number of edges that exit the loop.  */
  int num_exits;

  /* Bitmap of blocks that dominate all exits of the loop.  */
  sbitmap exits_doms;

  /* The loop nesting depth.  */
  int depth;

  /* Superloops of the loop.  */
  struct loop **pred;

  /* The height of the loop (enclosed loop levels) within the loop
     hierarchy tree.  */
  int level;

  /* The outer (parent) loop or NULL if outermost loop.  */
  struct loop *outer;

  /* The first inner (child) loop or NULL if innermost loop.  */
  struct loop *inner;

  /* Link to the next (sibling) loop.  */
  struct loop *next;

  /* Loop that is copy of this loop.  */
  struct loop *copy;

  /* Non-zero if the loop is invalid (e.g., contains setjmp.).  */
  int invalid;

  /* Auxiliary info specific to a pass.  */
  void *aux;

  /* The following are currently used by loop.c but they are likely to
     disappear as loop.c is converted to use the CFG.  */

  /* Non-zero if the loop has a NOTE_INSN_LOOP_VTOP.  */
  rtx vtop;

  /* Non-zero if the loop has a NOTE_INSN_LOOP_CONT.
     A continue statement will generate a branch to NEXT_INSN (cont).  */
  rtx cont;

  /* The dominator of cont.  */
  rtx cont_dominator;

  /* The NOTE_INSN_LOOP_BEG.  */
  rtx start;

  /* The NOTE_INSN_LOOP_END.  */
  rtx end;

  /* For a rotated loop that is entered near the bottom,
     this is the label at the top.  Otherwise it is zero.  */
  rtx top;

  /* Place in the loop where control enters.  */
  rtx scan_start;

  /* The position where to sink insns out of the loop.  */
  rtx sink;

  /* List of all LABEL_REFs which refer to code labels outside the
     loop.  Used by routines that need to know all loop exits, such as
     final_biv_value and final_giv_value.

     This does not include loop exits due to return instructions.
     This is because all bivs and givs are pseudos, and hence must be
     dead after a return, so the presence of a return does not affect
     any of the optimizations that use this info.  It is simpler to
     just not include return instructions on this list.  */
  rtx exit_labels;

  /* The number of LABEL_REFs on exit_labels for this loop and all
     loops nested inside it.  */
  int exit_count;
};

/* Flags for state of loop structure.  */
enum
{
  LOOPS_HAVE_PREHEADERS = 1,
  LOOPS_HAVE_SIMPLE_LATCHES = 2,
  LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4
};

/* Structure to hold CFG information about natural loops within a function.  */
struct loops
{
  /* Number of natural loops in the function.  */
  unsigned num;

  /* Maximum nested loop level in the function.  */
  unsigned levels;

  /* Array of natural loop descriptors (scanning this array in reverse order
     will find the inner loops before their enclosing outer loops).  */
  struct loop *array;

  /* The above array is unused in new loop infrastructure and is kept only for
     purposes of the old loop optimizer.  Instead we store just pointers to
     loops here.  */
  struct loop **parray;

  /* Pointer to root of loop hierarchy tree.  */
  struct loop *tree_root;

  /* Information derived from the CFG.  */
  struct cfg
  {
    /* The bitmap vector of dominators or NULL if not computed.  */
    dominance_info dom;

    /* The ordering of the basic blocks in a depth first search.  */
    int *dfs_order;

    /* The reverse completion ordering of the basic blocks found in a
       depth first search.  */
    int *rc_order;
  } cfg;

  /* Headers shared by multiple loops that should be merged.  */
  sbitmap shared_headers;

  /* State of loops.  */
  int state;
};

/* Flags for loop discovery.  */

#define LOOP_TREE		1	/* Build loop hierarchy tree.  */
#define LOOP_PRE_HEADER		2	/* Analyze loop preheader.  */
#define LOOP_ENTRY_EDGES	4	/* Find entry edges.  */
#define LOOP_EXIT_EDGES		8	/* Find exit edges.  */
#define LOOP_EDGES		(LOOP_ENTRY_EDGES | LOOP_EXIT_EDGES)
#define LOOP_ALL	       15	/* All of the above  */

/* Loop recognition.  */
extern int flow_loops_find		PARAMS ((struct loops *, int flags));
extern int flow_loops_update		PARAMS ((struct loops *, int flags));
extern void flow_loops_free		PARAMS ((struct loops *));
extern void flow_loops_dump		PARAMS ((const struct loops *, FILE *,
						void (*)(const struct loop *,
						FILE *, int), int));
extern void flow_loop_dump		PARAMS ((const struct loop *, FILE *,
						void (*)(const struct loop *,
						FILE *, int), int));
extern int flow_loop_scan		PARAMS ((struct loops *,
						struct loop *, int));
void mark_irreducible_loops		PARAMS ((struct loops *));

/* Loop datastructure manipulation/querying.  */
extern void flow_loop_tree_node_add	PARAMS ((struct loop *, struct loop *));
extern void flow_loop_tree_node_remove	PARAMS ((struct loop *));
extern bool flow_loop_outside_edge_p	PARAMS ((const struct loop *, edge));
extern bool flow_loop_nested_p		PARAMS ((const struct loop *,
						const struct loop *));
extern bool flow_bb_inside_loop_p	PARAMS ((const struct loop *,
						basic_block));
extern struct loop * find_common_loop	PARAMS ((struct loop *, struct loop *));
extern int num_loop_insns		PARAMS ((struct loop *));
extern int average_num_loop_insns	PARAMS ((struct loop *));

/* Loops & cfg manipulation.  */
extern basic_block *get_loop_body	PARAMS ((const struct loop *));

extern edge loop_preheader_edge		PARAMS ((const struct loop *));
extern edge loop_latch_edge		PARAMS ((const struct loop *));

extern void add_bb_to_loop		PARAMS ((basic_block, struct loop *));
extern void remove_bb_from_loops	PARAMS ((basic_block));

extern void cancel_loop			PARAMS ((struct loops *, struct loop *));
extern void cancel_loop_tree		PARAMS ((struct loops *, struct loop *));

extern basic_block loop_split_edge_with PARAMS ((edge, rtx, struct loops *));

enum
{
  CP_SIMPLE_PREHEADERS = 1,
  CP_INSIDE_CFGLAYOUT = 2
};

extern void create_preheaders		PARAMS ((struct loops *, int));
extern void force_single_succ_latches	PARAMS ((struct loops *));

extern void verify_loop_structure	PARAMS ((struct loops *));

/* Loop analysis.  */
extern bool simple_loop_p		PARAMS ((struct loops *, struct loop *,
						struct loop_desc *));
extern rtx count_loop_iterations	PARAMS ((struct loop_desc *, rtx, rtx));
extern bool just_once_each_iteration_p	PARAMS ((struct loops *,struct loop *,
						 basic_block));
extern unsigned expected_loop_iterations PARAMS ((const struct loop *));

cfgloopanal.c:
--------------
/* Natural loop analysis code for GNU compiler.
   Copyright (C) 2002, 2003 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "cfgloop.h"
#include "expr.h"
#include "output.h"

struct unmark_altered_insn_data;
static void unmark_altered	 PARAMS ((rtx, rtx, regset));
static void blocks_invariant_registers PARAMS ((basic_block *, int, regset));
static void unmark_altered_insn	 PARAMS ((rtx, rtx, struct unmark_altered_insn_data *));
static void blocks_single_set_registers PARAMS ((basic_block *, int, rtx *));
static int invariant_rtx_wrto_regs_p_helper PARAMS ((rtx *, regset));
static bool invariant_rtx_wrto_regs_p PARAMS ((rtx, regset));
static rtx test_for_iteration PARAMS ((struct loop_desc *desc,
				       unsigned HOST_WIDE_INT));
static bool constant_iterations PARAMS ((struct loop_desc *,
					 unsigned HOST_WIDE_INT *,
					 bool *));
static bool simple_loop_exit_p PARAMS ((struct loops *, struct loop *,
					edge, regset, rtx *,
					struct loop_desc *));
static rtx variable_initial_value PARAMS ((rtx, regset, rtx, rtx *));
static rtx variable_initial_values PARAMS ((edge, rtx));
static bool simple_condition_p PARAMS ((struct loop *, rtx,
					regset, struct loop_desc *));
static basic_block simple_increment PARAMS ((struct loops *, struct loop *,
					     rtx *, struct loop_desc *));

/* Checks whether BB is executed exactly once in each LOOP iteration.  */
bool
just_once_each_iteration_p (loops, loop, bb)
     struct loops *loops;
     struct loop *loop;
     basic_block bb;
{
  /* It must be executed at least once each iteration.  */
  if (!dominated_by_p (loops->cfg.dom, loop->latch, bb))
    return false;

  /* And just once.  */
  if (bb->loop_father != loop)
    return false;

  /* But this was not enough.  We might have some irreducible loop here.  */
  if (bb->flags & BB_IRREDUCIBLE_LOOP)
    return false;

  return true;
}


/* Unmarks modified registers; helper to blocks_invariant_registers.  */
static void
unmark_altered (what, by, regs)
     rtx what;
     rtx by ATTRIBUTE_UNUSED;
     regset regs;
{
  if (GET_CODE (what) == SUBREG)
    what = SUBREG_REG (what);
  if (!REG_P (what))
    return;
  CLEAR_REGNO_REG_SET (regs, REGNO (what));
}

/* Marks registers that are invariant inside blocks BBS.  */
static void
blocks_invariant_registers (bbs, nbbs, regs)
     basic_block *bbs;
     int nbbs;
     regset regs;
{
  rtx insn;
  int i;

  for (i = 0; i < max_reg_num (); i++)
    SET_REGNO_REG_SET (regs, i);
  for (i = 0; i < nbbs; i++)
    for (insn = bbs[i]->head;
	 insn != NEXT_INSN (bbs[i]->end);
	 insn = NEXT_INSN (insn))
      if (INSN_P (insn))
	note_stores (PATTERN (insn),
		     (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
		     regs);
}

/* Unmarks modified registers; helper to blocks_single_set_registers.  */
struct unmark_altered_insn_data
{
  rtx *regs;
  rtx insn;
};

static void
unmark_altered_insn (what, by, data)
     rtx what;
     rtx by ATTRIBUTE_UNUSED;
     struct unmark_altered_insn_data *data;
{
  int rn;

  if (GET_CODE (what) == SUBREG)
    what = SUBREG_REG (what);
  if (!REG_P (what))
    return;
  rn = REGNO (what);
  if (data->regs[rn] == data->insn)
    return;
  data->regs[rn] = NULL;
}

/* Marks registers that have just single simple set in BBS; the relevant
   insn is returned in REGS.  */
static void
blocks_single_set_registers (bbs, nbbs, regs)
     basic_block *bbs;
     int nbbs;
     rtx *regs;
{
  rtx insn;
  int i;
  struct unmark_altered_insn_data data;

  for (i = 0; i < max_reg_num (); i++)
    regs[i] = NULL;

  for (i = 0; i < nbbs; i++)
    for (insn = bbs[i]->head;
	 insn != NEXT_INSN (bbs[i]->end);
	 insn = NEXT_INSN (insn))
      {
	rtx set = single_set (insn);
	if (!set)
	  continue;
	if (!REG_P (SET_DEST (set)))
	  continue;
	regs[REGNO (SET_DEST (set))] = insn;
      }

  data.regs = regs;
  for (i = 0; i < nbbs; i++)
    for (insn = bbs[i]->head;
	 insn != NEXT_INSN (bbs[i]->end);
	 insn = NEXT_INSN (insn))
      {
        if (!INSN_P (insn))
	  continue;
	data.insn = insn;
	note_stores (PATTERN (insn),
	    (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered_insn,
	    &data);
      }
}

/* Helper for invariant_rtx_wrto_regs_p.  */
static int
invariant_rtx_wrto_regs_p_helper (expr, invariant_regs)
     rtx *expr;
     regset invariant_regs;
{
  switch (GET_CODE (*expr))
    {
    case CC0:
    case PC:
    case UNSPEC_VOLATILE:
      return 1;

    case CONST_INT:
    case CONST_DOUBLE:
    case CONST:
    case SYMBOL_REF:
    case LABEL_REF:
      return 0;

    case ASM_OPERANDS:
      return MEM_VOLATILE_P (*expr);

    case MEM:
      /* If the memory is not constant, assume it is modified.  If it is
	 constant, we still have to check the address.  */
      return !RTX_UNCHANGING_P (*expr);

    case REG:
      return !REGNO_REG_SET_P (invariant_regs, REGNO (*expr));

    default:
      return 0;
    }
}

/* Checks that EXPR is invariant provided that INVARIANT_REGS are invariant. */
static bool
invariant_rtx_wrto_regs_p (expr, invariant_regs)
     rtx expr;
     regset invariant_regs;
{
  return !for_each_rtx (&expr, (rtx_function) invariant_rtx_wrto_regs_p_helper,
			invariant_regs);
}

/* Checks whether CONDITION is a simple comparison in that one of operands
   is register and the other one is invariant in the LOOP. Fills var, lim
   and cond fields in DESC.  */
static bool
simple_condition_p (loop, condition, invariant_regs, desc)
     struct loop *loop ATTRIBUTE_UNUSED;
     rtx condition;
     regset invariant_regs;
     struct loop_desc *desc;
{
  rtx op0, op1;

  /* Check condition.  */
  switch (GET_CODE (condition))
    {
      case EQ:
      case NE:
      case LE:
      case LT:
      case GE:
      case GT:
      case GEU:
      case GTU:
      case LEU:
      case LTU:
	break;
      default:
	return false;
    }

  /* Of integers or pointers.  */
  if (GET_MODE_CLASS (GET_MODE (XEXP (condition, 0))) != MODE_INT
      && GET_MODE_CLASS (GET_MODE (XEXP (condition, 0))) != MODE_PARTIAL_INT)
    return false;

  /* One of operands must be a simple register.  */
  op0 = XEXP (condition, 0);
  op1 = XEXP (condition, 1);
  
  /* One of operands must be invariant.  */
  if (invariant_rtx_wrto_regs_p (op0, invariant_regs))
    {
      /* And the other one must be a register.  */
      if (!REG_P (op1))
	return false;
      desc->var = op1;
      desc->lim = op0;

      desc->cond = swap_condition (GET_CODE (condition));
      if (desc->cond == UNKNOWN)
	return false;
      return true;
    }

  /* Check the other operand. */
  if (!invariant_rtx_wrto_regs_p (op1, invariant_regs))
    return false;
  if (!REG_P (op0))
    return false;

  desc->var = op0;
  desc->lim = op1;

  desc->cond = GET_CODE (condition);

  return true;
}

/* Checks whether DESC->var is incremented/decremented exactly once each
   iteration.  Fills in DESC->stride and returns block in that DESC->var is
   modified.  */
static basic_block
simple_increment (loops, loop, simple_increment_regs, desc)
     struct loops *loops;
     struct loop *loop;
     rtx *simple_increment_regs;
     struct loop_desc *desc;
{
  rtx mod_insn, set, set_src, set_add;
  basic_block mod_bb;

  /* Find insn that modifies var.  */
  mod_insn = simple_increment_regs[REGNO (desc->var)];
  if (!mod_insn)
    return NULL;
  mod_bb = BLOCK_FOR_INSN (mod_insn);

  /* Check that it is executed exactly once each iteration.  */
  if (!just_once_each_iteration_p (loops, loop, mod_bb))
    return NULL;

  /* mod_insn must be a simple increment/decrement.  */
  set = single_set (mod_insn);
  if (!set)
    abort ();
  if (!rtx_equal_p (SET_DEST (set), desc->var))
    abort ();

  set_src = find_reg_equal_equiv_note (mod_insn);
  if (!set_src)
    set_src = SET_SRC (set);
  if (GET_CODE (set_src) != PLUS)
    return NULL;
  if (!rtx_equal_p (XEXP (set_src, 0), desc->var))
    return NULL;

  /* Set desc->stride.  */
  set_add = XEXP (set_src, 1);
  if (CONSTANT_P (set_add))
    desc->stride = set_add;
  else
    return NULL;

  return mod_bb;
}

/* Tries to find initial value of VAR in INSN.  This value must be invariant
   wrto INVARIANT_REGS.  If SET_INSN is not NULL, insn in that var is set is
   placed here.  */
static rtx
variable_initial_value (insn, invariant_regs, var, set_insn)
     rtx insn;
     regset invariant_regs;
     rtx var;
     rtx *set_insn;
{
  basic_block bb;
  rtx set;

  /* Go back through cfg.  */
  bb = BLOCK_FOR_INSN (insn);
  while (1)
    {
      for (; insn != bb->head; insn = PREV_INSN (insn))
	{
	  if (modified_between_p (var, PREV_INSN (insn), NEXT_INSN (insn)))
	    break;
	  if (INSN_P (insn))
	    note_stores (PATTERN (insn),
		(void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
		invariant_regs);
	}

      if (insn != bb->head)
	{
	  /* We found place where var is set.  */
	  rtx set_dest;
	  rtx val;
	  rtx note;
          
	  set = single_set (insn);
	  if (!set)
	    return NULL;
	  set_dest = SET_DEST (set);
	  if (!rtx_equal_p (set_dest, var))
	    return NULL;

	  note = find_reg_equal_equiv_note (insn);
	  if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
	    val = XEXP (note, 0);
	  else
	    val = SET_SRC (set);
	  if (!invariant_rtx_wrto_regs_p (val, invariant_regs))
	    return NULL;

	  if (set_insn)
	    *set_insn = insn;
	  return val;
	}


      if (bb->pred->pred_next || bb->pred->src == ENTRY_BLOCK_PTR)
	return NULL;

      bb = bb->pred->src;
      insn = bb->end;
    }

  return NULL;
}

/* Returns list of definitions of initial value of VAR at Edge.  */
static rtx
variable_initial_values (e, var)
     edge e;
     rtx var;
{
  rtx set_insn, list;
  regset invariant_regs;
  regset_head invariant_regs_head;
  int i;

  invariant_regs = INITIALIZE_REG_SET (invariant_regs_head);
  for (i = 0; i < max_reg_num (); i++)
    SET_REGNO_REG_SET (invariant_regs, i);

  list = alloc_EXPR_LIST (0, copy_rtx (var), NULL);

  if (e->src == ENTRY_BLOCK_PTR)
    return list;

  set_insn = e->src->end;
  while (REG_P (var)
	 && (var = variable_initial_value (set_insn, invariant_regs, var, &set_insn)))
    list = alloc_EXPR_LIST (0, copy_rtx (var), list);

  FREE_REG_SET (invariant_regs);
  return list;
}

/* Counts constant number of iterations of the loop described by DESC;
   returns false if impossible.  */
static bool
constant_iterations (desc, niter, may_be_zero)
     struct loop_desc *desc;
     unsigned HOST_WIDE_INT *niter;
     bool *may_be_zero;
{
  rtx test, expr;
  rtx ainit, alim;

  test = test_for_iteration (desc, 0);
  if (test == const0_rtx)
    {
      *niter = 0;
      *may_be_zero = false;
      return true;
    }

  *may_be_zero = (test != const_true_rtx);

  /* It would make a little sense to check every with every when we
     know that all but the first alternative are simply registers.  */
  for (ainit = desc->var_alts; ainit; ainit = XEXP (ainit, 1))
    {
      alim = XEXP (desc->lim_alts, 0);
      if (!(expr = count_loop_iterations (desc, XEXP (ainit, 0), alim)))
	abort ();
      if (GET_CODE (expr) == CONST_INT)
	{
	  *niter = INTVAL (expr);
	  return true;
	}
    }
  for (alim = XEXP (desc->lim_alts, 1); alim; alim = XEXP (alim, 1))
    {
      ainit = XEXP (desc->var_alts, 0);
      if (!(expr = count_loop_iterations (desc, ainit, XEXP (alim, 0))))
	abort ();
      if (GET_CODE (expr) == CONST_INT)
	{
	  *niter = INTVAL (expr);
	  return true;
	}
    }

  return false;
}

/* Return RTX expression representing number of iterations of loop as bounded
   by test described by DESC (in the case loop really has multiple exit
   edges, fewer iterations may happen in the practice).  

   Return NULL if it is unknown.  Additionally the value may be invalid for
   paradoxical loop (lets define paradoxical loops as loops whose test is
   failing at -1th iteration, for instance "for (i=5;i<1;i++);").
   
   These cases needs to be either cared by copying the loop test in the front
   of loop or keeping the test in first iteration of loop.
   
   When INIT/LIM are set, they are used instead of var/lim of DESC. */
rtx
count_loop_iterations (desc, init, lim)
     struct loop_desc *desc;
     rtx init;
     rtx lim;
{
  enum rtx_code cond = desc->cond;
  rtx stride = desc->stride;
  rtx mod, exp;

  /* Give up on floating point modes and friends.  It can be possible to do
     the job for constant loop bounds, but it is probably not worthwhile.  */
  if (!INTEGRAL_MODE_P (GET_MODE (desc->var)))
    return NULL;

  init = copy_rtx (init ? init : desc->var);
  lim = copy_rtx (lim ? lim : desc->lim);

  /* Ensure that we always handle the condition to stay inside loop.  */
  if (desc->neg)
    cond = reverse_condition (cond);

  /* Compute absolute value of the difference of initial and final value.  */
  if (INTVAL (stride) > 0)
    {
      /* Bypass nonsensical tests.  */
      if (cond == EQ || cond == GE || cond == GT || cond == GEU
	  || cond == GTU)
	return NULL;
      exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
				 lim, init);
    }
  else
    {
      /* Bypass nonsensical tests.  */
      if (cond == EQ || cond == LE || cond == LT || cond == LEU
	  || cond == LTU)
	return NULL;
      exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
				 init, lim);
      stride = simplify_gen_unary (NEG, GET_MODE (desc->var),
				   stride, GET_MODE (desc->var));
    }

  /* Normalize difference so the value is always first examined
     and later incremented.  */

  if (!desc->postincr)
    exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
			       exp, stride);

  /* Determine delta caused by exit condition.  */
  switch (cond)
    {
    case NE:
      /* For NE tests, make sure that the iteration variable won't miss
	 the final value.  If EXP mod STRIDE is not zero, then the
	 iteration variable will overflow before the loop exits, and we
	 can not calculate the number of iterations easily.  */
      if (stride != const1_rtx
	  && (simplify_gen_binary (UMOD, GET_MODE (desc->var), exp, stride)
              != const0_rtx))
	return NULL;
      break;
    case LT:
    case GT:
    case LTU:
    case GTU:
      break;
    case LE:
    case GE:
    case LEU:
    case GEU:
      exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
				 exp, const1_rtx);
      break;
    default:
      abort ();
    }

  if (stride != const1_rtx)
    {
      /* Number of iterations is now (EXP + STRIDE - 1 / STRIDE),
	 but we need to take care for overflows.   */

      mod = simplify_gen_binary (UMOD, GET_MODE (desc->var), exp, stride);

      /* This is dirty trick.  When we can't compute number of iterations
	 to be constant, we simply ignore the possible overflow, as
	 runtime unroller always use power of 2 amounts and does not
	 care about possible lost bits.  */

      if (GET_CODE (mod) != CONST_INT)
	{
	  rtx stridem1 = simplify_gen_binary (PLUS, GET_MODE (desc->var),
					      stride, constm1_rtx);
	  exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
				     exp, stridem1);
	  exp = simplify_gen_binary (UDIV, GET_MODE (desc->var), exp, stride);
	}
      else
	{
	  exp = simplify_gen_binary (UDIV, GET_MODE (desc->var), exp, stride);
	  if (mod != const0_rtx)
	    exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
				       exp, const1_rtx);
	}
    }

  if (rtl_dump_file)
    {
      fprintf (rtl_dump_file, ";  Number of iterations: ");
      print_simple_rtl (rtl_dump_file, exp);
      fprintf (rtl_dump_file, "\n");
    }

  return exp;
}

/* Return simplified RTX expression representing the value of test
   described of DESC at given iteration of loop.  */

static rtx
test_for_iteration (desc, iter)
     struct loop_desc *desc;
     unsigned HOST_WIDE_INT iter;
{
  enum rtx_code cond = desc->cond;
  rtx exp = XEXP (desc->var_alts, 0);
  rtx addval;

  /* Give up on floating point modes and friends.  It can be possible to do
     the job for constant loop bounds, but it is probably not worthwhile.  */
  if (!INTEGRAL_MODE_P (GET_MODE (desc->var)))
    return NULL;

  /* Ensure that we always handle the condition to stay inside loop.  */
  if (desc->neg)
    cond = reverse_condition (cond);

  /* Compute the value of induction variable.  */
  addval = simplify_gen_binary (MULT, GET_MODE (desc->var),
				desc->stride,
				gen_int_mode (desc->postincr
					      ? iter : iter + 1,
					      GET_MODE (desc->var)));
  exp = simplify_gen_binary (PLUS, GET_MODE (desc->var), exp, addval);
  /* Test at given condition.  */
  exp = simplify_gen_relational (cond, SImode,
				 GET_MODE (desc->var), exp, desc->lim);

  if (rtl_dump_file)
    {
      fprintf (rtl_dump_file, ";  Conditional to continue loop at ");
      fprintf (rtl_dump_file, HOST_WIDE_INT_PRINT_UNSIGNED, iter);
      fprintf (rtl_dump_file, "th iteration: ");
      print_simple_rtl (rtl_dump_file, exp);
      fprintf (rtl_dump_file, "\n");
    }
  return exp;
}


/* Tests whether exit at EXIT_EDGE from LOOP is simple.  Returns simple loop
   description joined to it in in DESC.  INVARIANT_REGS and SINGLE_SET_REGS
   are results of blocks_{invariant,single_set}_regs over BODY.  */
static bool
simple_loop_exit_p (loops, loop, exit_edge, invariant_regs, single_set_regs, desc)
     struct loops *loops;
     struct loop *loop;
     edge exit_edge;
     struct loop_desc *desc;
     regset invariant_regs;
     rtx *single_set_regs;
{
  basic_block mod_bb, exit_bb;
  int fallthru_out;
  rtx condition;
  edge ei, e;

  exit_bb = exit_edge->src;

  fallthru_out = (exit_edge->flags & EDGE_FALLTHRU);

  if (!exit_bb)
    return false;

  /* It must be tested (at least) once during any iteration.  */
  if (!dominated_by_p (loops->cfg.dom, loop->latch, exit_bb))
    return false;

  /* It must end in a simple conditional jump.  */
  if (!any_condjump_p (exit_bb->end))
    return false;

  ei = exit_bb->succ;
  if (ei == exit_edge)
    ei = ei->succ_next;

  desc->out_edge = exit_edge;
  desc->in_edge = ei;

  /* Condition must be a simple comparison in that one of operands
     is register and the other one is invariant.  */
  if (!(condition = get_condition (exit_bb->end, NULL)))
    return false;

  if (!simple_condition_p (loop, condition, invariant_regs, desc))
    return false;

  /*  Var must be simply incremented or decremented in exactly one insn that
     is executed just once every iteration.  */
  if (!(mod_bb = simple_increment (loops, loop, single_set_regs, desc)))
    return false;

  /* OK, it is simple loop.  Now just fill in remaining info.  */
  desc->postincr = !dominated_by_p (loops->cfg.dom, exit_bb, mod_bb);
  desc->neg = !fallthru_out;

  /* Find initial value of var and alternative values for lim.  */
  e = loop_preheader_edge (loop);
  desc->var_alts = variable_initial_values (e, desc->var);
  desc->lim_alts = variable_initial_values (e, desc->lim);

  /* Number of iterations. */
  if (!count_loop_iterations (desc, NULL, NULL))
    return false;
  desc->const_iter =
    constant_iterations (desc, &desc->niter, &desc->may_be_zero);
  return true;
}

/* Tests whether LOOP is simple for loop.  Returns simple loop description
   in DESC.  */
bool
simple_loop_p (loops, loop, desc)
     struct loops *loops;
     struct loop *loop;
     struct loop_desc *desc;
{
  unsigned i;
  basic_block *body;
  edge e;
  struct loop_desc act;
  bool any = false;
  regset invariant_regs;
  regset_head invariant_regs_head;
  rtx *single_set_regs;
  int n_branches;
  
  body = get_loop_body (loop);

  invariant_regs = INITIALIZE_REG_SET (invariant_regs_head);
  single_set_regs = xmalloc (max_reg_num () * sizeof (rtx));

  blocks_invariant_registers (body, loop->num_nodes, invariant_regs);
  blocks_single_set_registers (body, loop->num_nodes, single_set_regs);

  n_branches = 0;
  for (i = 0; i < loop->num_nodes; i++)
    {
      for (e = body[i]->succ; e; e = e->succ_next)
	if (!flow_bb_inside_loop_p (loop, e->dest)
	    && simple_loop_exit_p (loops, loop, e,
		   invariant_regs, single_set_regs, &act))
	  {
	    /* Prefer constant iterations; the less the better.  */
	    if (!any)
	      any = true;
	    else if (!act.const_iter
		     || (desc->const_iter && act.niter >= desc->niter))
	      continue;
	    *desc = act;
	  }

      if (body[i]->succ && body[i]->succ->succ_next)
	n_branches++;
    }
  desc->n_branches = n_branches;

  if (rtl_dump_file && any)
    {
      fprintf (rtl_dump_file, "; Simple loop %i\n", loop->num);
      if (desc->postincr)
	fprintf (rtl_dump_file,
		 ";  does postincrement after loop exit condition\n");

      fprintf (rtl_dump_file, ";  Induction variable:");
      print_simple_rtl (rtl_dump_file, desc->var);
      fputc ('\n', rtl_dump_file);

      fprintf (rtl_dump_file, ";  Initial values:");
      print_simple_rtl (rtl_dump_file, desc->var_alts);
      fputc ('\n', rtl_dump_file);

      fprintf (rtl_dump_file, ";  Stride:");
      print_simple_rtl (rtl_dump_file, desc->stride);
      fputc ('\n', rtl_dump_file);

      fprintf (rtl_dump_file, ";  Compared with:");
      print_simple_rtl (rtl_dump_file, desc->lim);
      fputc ('\n', rtl_dump_file);

      fprintf (rtl_dump_file, ";  Alternative values:");
      print_simple_rtl (rtl_dump_file, desc->lim_alts);
      fputc ('\n', rtl_dump_file);

      fprintf (rtl_dump_file, ";  Exit condition:");
      if (desc->neg)
	fprintf (rtl_dump_file, "(negated)");
      fprintf (rtl_dump_file, "%s\n", GET_RTX_NAME (desc->cond));

      fprintf (rtl_dump_file, ";  Number of branches:");
      fprintf (rtl_dump_file, "%d\n", desc->n_branches);

      fputc ('\n', rtl_dump_file);
    }

  free (body);
  FREE_REG_SET (invariant_regs);
  free (single_set_regs);
  return any;
}

/* Marks blocks that are part of non-recognized loops; i.e. we throw away
   all latch edges and mark blocks inside any remaining cycle.  Everything
   is a bit complicated due to fact we do not want to do this for parts of
   cycles that only "pass" through some loop -- i.e. for each cycle, we want
   to mark blocks that belong directly to innermost loop containing the whole
   cycle.  */
void
mark_irreducible_loops (loops)
     struct loops *loops;
{
  int *dfs_in, *closed, *mr, *mri, *n_edges, *stack;
  unsigned i;
  edge **edges, e;
  basic_block act;
  int stack_top, tick, depth;
  struct loop *cloop;

  /* The first last_basic_block + 1 entries are for real blocks (including
     entry); then we have loops->num - 1 fake blocks for loops to that we
     assign edges leading from loops (fake loop 0 is not interesting).  */
  dfs_in = xmalloc ((last_basic_block + loops->num) * sizeof (int));
  closed = xmalloc ((last_basic_block + loops->num) * sizeof (int));
  mr = xmalloc ((last_basic_block + loops->num) * sizeof (int));
  mri = xmalloc ((last_basic_block + loops->num) * sizeof (int));
  n_edges = xmalloc ((last_basic_block + loops->num) * sizeof (int));
  edges = xmalloc ((last_basic_block + loops->num) * sizeof (edge *));
  stack = xmalloc ((n_basic_blocks + loops->num) * sizeof (int));

  /* Create the edge lists.  */
  for (i = 0; i < last_basic_block + loops->num; i++)
    n_edges[i] = 0;
  FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
    for (e = act->succ; e; e = e->succ_next)
      {
        /* Ignore edges to exit.  */
        if (e->dest == EXIT_BLOCK_PTR)
	  continue;
	/* And latch edges.  */
	if (e->dest->loop_father->header == e->dest
	    && e->dest->loop_father->latch == act)
	  continue;
	/* Edges inside a single loop should be left where they are.  Edges
	   to subloop headers should lead to representative of the subloop,
	   but from the same place.  */
	if (act->loop_father == e->dest->loop_father
	    || act->loop_father == e->dest->loop_father->outer)
	  {
	    n_edges[act->index + 1]++;
	    continue;
	  }
	/* Edges exiting loops remain.  They should lead from representative
	   of the son of nearest common ancestor of the loops in that
	   act lays.  */
	depth = find_common_loop (act->loop_father, e->dest->loop_father)->depth + 1;
	if (depth == act->loop_father->depth)
	  cloop = act->loop_father;
	else
	  cloop = act->loop_father->pred[depth];
	n_edges[cloop->num + last_basic_block]++;
      }

  for (i = 0; i < last_basic_block + loops->num; i++)
    {
      edges[i] = xmalloc (n_edges[i] * sizeof (edge));
      n_edges[i] = 0;
    }

  FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
    for (e = act->succ; e; e = e->succ_next)
      {
        if (e->dest == EXIT_BLOCK_PTR)
	  continue;
	if (e->dest->loop_father->header == e->dest
	    && e->dest->loop_father->latch == act)
	  continue;
	if (act->loop_father == e->dest->loop_father
	    || act->loop_father == e->dest->loop_father->outer)
	  {
	    edges[act->index + 1][n_edges[act->index + 1]++] = e;
	    continue;
	  }
	depth = find_common_loop (act->loop_father, e->dest->loop_father)->depth + 1;
	if (depth == act->loop_father->depth)
	  cloop = act->loop_father;
	else
	  cloop = act->loop_father->pred[depth];
	i = cloop->num + last_basic_block;
	edges[i][n_edges[i]++] = e;
      }

  /* Compute dfs numbering, starting from loop headers, and mark found
     loops.*/
  tick = 0;
  for (i = 0; i < last_basic_block + loops->num; i++)
    {
      dfs_in[i] = -1;
      closed[i] = 0;
      mr[i] = last_basic_block + loops->num;
      mri[i] = -1;
    }

  stack_top = 0;
  for (i = 0; i < loops->num; i++)
    if (loops->parray[i])
      stack[stack_top++] = loops->parray[i]->header->index + 1;

  while (stack_top)
    {
      int idx, sidx;

      idx = stack[stack_top - 1];
      if (dfs_in[idx] < 0)
	dfs_in[idx] = tick++;

      while (n_edges[idx])
	{
	  e = edges[idx][--n_edges[idx]];
	  sidx = e->dest->loop_father->header == e->dest
	           ? e->dest->loop_father->num + last_basic_block
	           : e->dest->index + 1;
          if (closed[sidx])
	    {
	      if (mr[sidx] < mr[idx] && !closed[mri[sidx]])
		{
		  mr[idx] = mr[sidx];
		  mri[idx] = mri[sidx];
		}
	      continue;
	    }
	  if (dfs_in[sidx] < 0)
	    {
	      stack[stack_top++] = sidx;
	      goto next;
	    }
	  if (dfs_in[sidx] < mr[idx])
	    {
	      mr[idx] = dfs_in[sidx];
	      mri[idx] = sidx;
	    }
	}

      /* Return back.  */
      closed[idx] = 1;
      stack_top--;
      if (stack_top && dfs_in[stack[stack_top - 1]] >= 0)
        {
	  /* Propagate information back.  */
	  sidx = stack[stack_top - 1];
	  if (mr[sidx] > mr[idx])
	    {
	      mr[sidx] = mr[idx];
	      mri[sidx] = mri[idx];
	    }
	}
      /* Mark the block if relevant.  */
      if (idx && idx <= last_basic_block && mr[idx] <= dfs_in[idx])
        BASIC_BLOCK (idx - 1)->flags |= BB_IRREDUCIBLE_LOOP;
next:;
    }

  free (stack);
  free (dfs_in);
  free (closed);
  free (mr);
  free (mri);
  for (i = 0; i < last_basic_block + loops->num; i++)
    free (edges[i]);
  free (edges);
  free (n_edges);
  loops->state |= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS;
}

/* Counts number of insns inside LOOP.  */
int
num_loop_insns (loop)
     struct loop *loop;
{
  basic_block *bbs, bb;
  unsigned i, ninsns = 0;
  rtx insn;

  bbs = get_loop_body (loop);
  for (i = 0; i < loop->num_nodes; i++)
    {
      bb = bbs[i];
      ninsns++;
      for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
	ninsns++;
    }
  free(bbs);
  
  return ninsns;
}

/* Counts number of insns executed on average per iteration LOOP.  */
int
average_num_loop_insns (loop)
     struct loop *loop;
{
  basic_block *bbs, bb;
  unsigned i, binsns, ninsns, ratio;
  rtx insn;

  ninsns = 0;
  bbs = get_loop_body (loop);
  for (i = 0; i < loop->num_nodes; i++)
    {
      bb = bbs[i];

      binsns = 1;
      for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
	binsns++;

      ratio = loop->header->frequency == 0
	      ? BB_FREQ_MAX
	      : (bb->frequency * BB_FREQ_MAX) / loop->header->frequency;
      ninsns += binsns * ratio;
    }
  free(bbs);
 
  ninsns /= BB_FREQ_MAX;
  if (!ninsns)
    ninsns = 1; /* To avoid division by zero.  */

  return ninsns;
}

/* Returns expected number of LOOP iterations.
   Compute upper bound on number of iterations in case they do not fit integer
   to help loop peeling heuristics.  Use exact counts if at all possible.  */
unsigned
expected_loop_iterations (loop)
     const struct loop *loop;
{
  edge e;

  if (loop->header->count)
    {
      gcov_type count_in, count_latch, expected;

      count_in = 0;
      count_latch = 0;

      for (e = loop->header->pred; e; e = e->pred_next)
	if (e->src == loop->latch)
	  count_latch = e->count;
	else
	  count_in += e->count;

      if (count_in == 0)
	return 0;

      expected = (count_latch + count_in - 1) / count_in;

      /* Avoid overflows.  */
      return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected);
    }
  else
    {
      int freq_in, freq_latch;

      freq_in = 0;
      freq_latch = 0;

      for (e = loop->header->pred; e; e = e->pred_next)
	if (e->src == loop->latch)
	  freq_latch = EDGE_FREQUENCY (e);
	else
	  freq_in += EDGE_FREQUENCY (e);

      if (freq_in == 0)
	return 0;

      return (freq_latch + freq_in - 1) / freq_in;
    }
}

cfgloopmanip.c:
---------------
/* Loop manipulation code for GNU compiler.
   Copyright (C) 2002, 2003 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "cfgloop.h"
#include "cfglayout.h"
#include "output.h"

static basic_block create_preheader	PARAMS ((struct loop *, dominance_info,
						int));

/* Creates a pre-header for a LOOP.  Returns newly created block.  Unless
   CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
   entry; otherwise we also force preheader block to have only one successor.
   */
static basic_block
create_preheader (loop, dom, flags)
     struct loop *loop;
     dominance_info dom;
     int flags;
{
  edge e, fallthru;
  basic_block dummy;
  basic_block jump, src;
  struct loop *cloop, *ploop;
  int nentry = 0;
  rtx insn;

  cloop = loop->outer;

  for (e = loop->header->pred; e; e = e->pred_next)
    {
      if (e->src == loop->latch)
	continue;
      nentry++;
    }
  if (!nentry)
    abort ();
  if (nentry == 1)
    {
      for (e = loop->header->pred; e->src == loop->latch; e = e->pred_next);
      if (!(flags & CP_SIMPLE_PREHEADERS)
	  || !e->src->succ->succ_next)
	return NULL;
    }

  insn = first_insn_after_basic_block_note (loop->header);
  if (insn)
    insn = PREV_INSN (insn);
  else
    insn = get_last_insn ();
  if (insn == loop->header->end)
    {
      /* Split_block would not split block after its end.  */
      emit_note_after (NOTE_INSN_DELETED, insn);
    }
  if (flags & CP_INSIDE_CFGLAYOUT)
    fallthru = cfg_layout_split_block (loop->header, insn);
  else
    fallthru = split_block (loop->header, insn);
  dummy = fallthru->src;
  loop->header = fallthru->dest;

  /* The header could be a latch of some superloop(s); due to design of
     split_block, it would now move to fallthru->dest.  */
  for (ploop = loop; ploop; ploop = ploop->outer)
    if (ploop->latch == dummy)
      ploop->latch = fallthru->dest;

  add_to_dominance_info (dom, fallthru->dest);
  
  /* Redirect edges. */
  for (e = dummy->pred; e; e = e->pred_next)
    {
      src = e->src;
      if (src == loop->latch)
	break;
    }
  if (!e)
    abort ();

  dummy->frequency -= EDGE_FREQUENCY (e);
  dummy->count -= e->count;
  fallthru->count -= e->count;
  if (flags & CP_INSIDE_CFGLAYOUT)
    cfg_layout_redirect_edge (e, loop->header);
  else
    {
      jump = redirect_edge_and_branch_force (e, loop->header);
      if (jump)
	{
	  add_to_dominance_info (dom, jump);
	  set_immediate_dominator (dom, jump, src);
	  add_bb_to_loop (jump, loop);
	  loop->latch = jump;
	}
    }

  /* Update structures.  */
  redirect_immediate_dominators (dom, dummy, loop->header);
  set_immediate_dominator (dom, loop->header, dummy);
  loop->header->loop_father = loop;
  add_bb_to_loop (dummy, cloop);
  if (rtl_dump_file)
    fprintf (rtl_dump_file, "Created preheader block for loop %i\n",
	     loop->num);

  return dummy;
}

/* Create preheaders for each loop; for meaning of flags see
   create_preheader.  */
void
create_preheaders (loops, flags)
     struct loops *loops;
     int flags;
{
  unsigned i;
  for (i = 1; i < loops->num; i++)
    create_preheader (loops->parray[i], loops->cfg.dom, flags);
  loops->state |= LOOPS_HAVE_PREHEADERS;
}

/* Forces all loop latches to have only single successor.  */
void
force_single_succ_latches (loops)
     struct loops *loops;
{
  unsigned i;
  struct loop *loop;
  edge e;

  for (i = 1; i < loops->num; i++)
    {
      loop = loops->parray[i];
      if (!loop->latch->succ->succ_next)
	continue;
 
      for (e = loop->header->pred; e->src != loop->latch; e = e->pred_next);
	loop_split_edge_with (e, NULL_RTX, loops);
    }
  loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
}

/* A quite stupid function to put INSNS on E. They are supposed to form
   just one basic block. Jumps out are not handled, so cfg do not have to
   be ok after this function.  */
basic_block
loop_split_edge_with (e, insns, loops)
     edge e;
     rtx insns;
     struct loops *loops;
{
  basic_block src, dest, new_bb;
  struct loop *loop_c;
  edge new_e;
  
  src = e->src;
  dest = e->dest;

  loop_c = find_common_loop (src->loop_father, dest->loop_father);

  /* Create basic block for it.  */

  new_bb = create_basic_block (NULL_RTX, NULL_RTX, EXIT_BLOCK_PTR->prev_bb);
  add_to_dominance_info (loops->cfg.dom, new_bb);
  add_bb_to_loop (new_bb, loop_c);
  new_bb->flags = insns ? BB_SUPERBLOCK : 0;
  if (src->flags & BB_IRREDUCIBLE_LOOP)
    {
      /* We expect simple preheaders here.  */
      if ((dest->flags & BB_IRREDUCIBLE_LOOP)
          || dest->loop_father->header == dest)
        new_bb->flags |= BB_IRREDUCIBLE_LOOP;
    }

  new_e = make_edge (new_bb, dest, EDGE_FALLTHRU);
  new_e->probability = REG_BR_PROB_BASE;
  new_e->count = e->count;

  new_bb->count = e->count;
  new_bb->frequency = EDGE_FREQUENCY (e);
  cfg_layout_redirect_edge (e, new_bb);

  alloc_aux_for_block (new_bb, sizeof (struct reorder_block_def));
  if (insns)
    {
      start_sequence ();
      emit_insn (insns);
      insns = get_insns ();
      end_sequence ();
      emit_insn_after (insns, new_bb->end);
    }

  set_immediate_dominator (loops->cfg.dom, new_bb, src);
  set_immediate_dominator (loops->cfg.dom, dest,
    recount_dominator (loops->cfg.dom, dest));

  if (dest->loop_father->latch == src)
    dest->loop_father->latch = new_bb;
  
  return new_bb;
}



More information about the Gcc-patches mailing list