struct loops * loop_optimizer_init (FILE *dumpfile) { struct loops *loops = xcalloc (1, sizeof (struct loops)); edge e; edge_iterator ei; static bool first_time = true; if (first_time) { first_time = false; init_set_costs (); } /* Avoid annoying special cases of edges going to exit block. */ for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); ) if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src)) split_edge (e); else ei_next (&ei); /* Find the loops. */ if (flow_loops_find (loops) <= 1) { /* No loops. */ flow_loops_free (loops); free (loops); return NULL; } /* Not going to update these. */ free (loops->cfg.rc_order); loops->cfg.rc_order = NULL; free (loops->cfg.dfs_order); loops->cfg.dfs_order = NULL; /* Create pre-headers. */ create_preheaders (loops, CP_SIMPLE_PREHEADERS); /* Force all latches to have only single successor. */ force_single_succ_latches (loops); /* Mark irreducible loops. */ mark_irreducible_loops (loops); /* Dump loops. */ flow_loops_dump (loops, dumpfile, NULL, 1); #ifdef ENABLE_CHECKING verify_dominators (CDI_DOMINATORS); verify_loop_structure (loops); #endif return loops; }
static void apply_loop_flags (unsigned flags) { if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES) { /* If the loops may have multiple latches, we cannot canonicalize them further (and most of the loop manipulation functions will not work). However, we avoid modifying cfg, which some passes may want. */ gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES | LOOPS_HAVE_RECORDED_EXITS)) == 0); loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES); } else disambiguate_loops_with_multiple_latches (); /* Create pre-headers. */ if (flags & LOOPS_HAVE_PREHEADERS) { int cp_flags = CP_SIMPLE_PREHEADERS; if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS) cp_flags |= CP_FALLTHRU_PREHEADERS; create_preheaders (cp_flags); } /* Force all latches to have only single successor. */ if (flags & LOOPS_HAVE_SIMPLE_LATCHES) force_single_succ_latches (); /* Mark irreducible loops. */ if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) mark_irreducible_loops (); if (flags & LOOPS_HAVE_RECORDED_EXITS) record_loop_exits (); }
static funct_state analyze_function (struct cgraph_node *fn, bool ipa) { tree decl = fn->symbol.decl; funct_state l; basic_block this_block; l = XCNEW (struct funct_state_d); l->pure_const_state = IPA_CONST; l->state_previously_known = IPA_NEITHER; l->looping_previously_known = true; l->looping = false; l->can_throw = false; state_from_flags (&l->state_previously_known, &l->looping_previously_known, flags_from_decl_or_type (fn->symbol.decl), cgraph_node_cannot_return (fn)); if (fn->thunk.thunk_p || fn->symbol.alias) { /* Thunk gets propagated through, so nothing interesting happens. */ gcc_assert (ipa); return l; } if (dump_file) { fprintf (dump_file, "\n\n local analysis of %s\n ", cgraph_node_name (fn)); } push_cfun (DECL_STRUCT_FUNCTION (decl)); FOR_EACH_BB (this_block) { gimple_stmt_iterator gsi; struct walk_stmt_info wi; memset (&wi, 0, sizeof(wi)); for (gsi = gsi_start_bb (this_block); !gsi_end_p (gsi); gsi_next (&gsi)) { check_stmt (&gsi, l, ipa); if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw) goto end; } } end: if (l->pure_const_state != IPA_NEITHER) { /* Const functions cannot have back edges (an indication of possible infinite loop side effect. */ if (mark_dfs_back_edges ()) { /* Preheaders are needed for SCEV to work. Simple latches and recorded exits improve chances that loop will proved to be finite in testcases such as in loop-15.c and loop-24.c */ loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES | LOOPS_HAVE_RECORDED_EXITS); if (dump_file && (dump_flags & TDF_DETAILS)) flow_loops_dump (dump_file, NULL, 0); if (mark_irreducible_loops ()) { if (dump_file) fprintf (dump_file, " has irreducible loops\n"); l->looping = true; } else { loop_iterator li; struct loop *loop; scev_initialize (); FOR_EACH_LOOP (li, loop, 0) if (!finite_loop_p (loop)) { if (dump_file) fprintf (dump_file, " can not prove finiteness of " "loop %i\n", loop->num); l->looping =true; FOR_EACH_LOOP_BREAK (li); } scev_finalize (); } loop_optimizer_finalize (); } } if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, " checking previously known:"); better_state (&l->pure_const_state, &l->looping, l->state_previously_known, l->looping_previously_known); if (TREE_NOTHROW (decl)) l->can_throw = false; pop_cfun (); if (dump_file) { if (l->looping) fprintf (dump_file, "Function is locally looping.\n"); if (l->can_throw) fprintf (dump_file, "Function is locally throwing.\n"); if (l->pure_const_state == IPA_CONST) fprintf (dump_file, "Function is locally const.\n"); if (l->pure_const_state == IPA_PURE) fprintf (dump_file, "Function is locally pure.\n"); } return l; }
/* Checks that LOOPS are all right: -- sizes of loops are all right -- 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 (struct loops *loops) { unsigned *sizes, i, j; sbitmap irreds; basic_block *bbs, bb; struct loop *loop; int err = 0; edge e; /* Check sizes. */ sizes = xcalloc (loops->num, sizeof (int)); sizes[0] = 2; FOR_EACH_BB (bb) for (loop = bb->loop_father; loop; loop = loop->outer) sizes[loop->num]++; for (i = 0; i < loops->num; i++) { if (!loops->parray[i]) continue; if (loops->parray[i]->num_nodes != sizes[i]) { error ("Size of loop %d should be %d, not %d.", i, sizes[i], loops->parray[i]->num_nodes); err = 1; } } free (sizes); /* Check get_loop_body. */ for (i = 1; i < loops->num; i++) { loop = loops->parray[i]; if (!loop) continue; bbs = get_loop_body (loop); for (j = 0; j < loop->num_nodes; j++) if (!flow_bb_inside_loop_p (loop, bbs[j])) { error ("Bb %d do not belong to loop %d.", bbs[j]->index, i); err = 1; } free (bbs); } /* Check headers and latches. */ for (i = 1; i < loops->num; i++) { loop = loops->parray[i]; 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) { error ("Loop %d's latch does not have exactly 1 successor.", i); err = 1; } if (loop->latch->succ->dest != loop->header) { error ("Loop %d's latch does not have header as successor.", i); err = 1; } if (loop->latch->loop_father != loop) { error ("Loop %d's latch does not belong directly to it.", i); err = 1; } } if (loop->header->loop_father != loop) { error ("Loop %d's header does not belong directly to it.", i); err = 1; } if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)) { error ("Loop %d's latch is marked as part of irreducible region.", i); err = 1; } } /* 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); for (e = bb->succ; e; e = e->succ_next) if (e->flags & EDGE_IRREDUCIBLE_LOOP) e->flags |= EDGE_ALL_FLAGS + 1; } /* 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; } for (e = bb->succ; e; e = e->succ_next) { if ((e->flags & EDGE_IRREDUCIBLE_LOOP) && !(e->flags & (EDGE_ALL_FLAGS + 1))) { error ("Edge from %d to %d should be marked irreducible.", e->src->index, e->dest->index); err = 1; } else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP) && (e->flags & (EDGE_ALL_FLAGS + 1))) { error ("Edge from %d to %d should not be marked irreducible.", e->src->index, e->dest->index); err = 1; } e->flags &= ~(EDGE_ALL_FLAGS + 1); } } free (irreds); }