1*e4b17023SJohn Marino /* Code to analyze doloop loops in order for targets to perform late 2*e4b17023SJohn Marino optimizations converting doloops to other forms of hardware loops. 3*e4b17023SJohn Marino Copyright (C) 2011 Free Software Foundation, Inc. 4*e4b17023SJohn Marino 5*e4b17023SJohn Marino This file is part of GCC. 6*e4b17023SJohn Marino 7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under 8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free 9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later 10*e4b17023SJohn Marino version. 11*e4b17023SJohn Marino 12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or 14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15*e4b17023SJohn Marino for more details. 16*e4b17023SJohn Marino 17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License 18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see 19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */ 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino /* We need to keep a vector of loops */ 22*e4b17023SJohn Marino typedef struct hwloop_info_d *hwloop_info; 23*e4b17023SJohn Marino DEF_VEC_P (hwloop_info); 24*e4b17023SJohn Marino DEF_VEC_ALLOC_P (hwloop_info,heap); 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino /* Information about a loop we have found (or are in the process of 27*e4b17023SJohn Marino finding). */ 28*e4b17023SJohn Marino struct GTY (()) hwloop_info_d 29*e4b17023SJohn Marino { 30*e4b17023SJohn Marino /* loop number, for dumps */ 31*e4b17023SJohn Marino int loop_no; 32*e4b17023SJohn Marino 33*e4b17023SJohn Marino /* Next loop in the graph. */ 34*e4b17023SJohn Marino hwloop_info next; 35*e4b17023SJohn Marino 36*e4b17023SJohn Marino /* Vector of blocks only within the loop, including those within 37*e4b17023SJohn Marino inner loops. */ 38*e4b17023SJohn Marino VEC (basic_block, heap) *blocks; 39*e4b17023SJohn Marino 40*e4b17023SJohn Marino /* Same information in a bitmap. */ 41*e4b17023SJohn Marino bitmap block_bitmap; 42*e4b17023SJohn Marino 43*e4b17023SJohn Marino /* Vector of inner loops within this loop. Includes loops of every 44*e4b17023SJohn Marino nesting level. */ 45*e4b17023SJohn Marino VEC (hwloop_info, heap) *loops; 46*e4b17023SJohn Marino 47*e4b17023SJohn Marino /* All edges that jump into the loop. */ 48*e4b17023SJohn Marino VEC(edge, gc) *incoming; 49*e4b17023SJohn Marino 50*e4b17023SJohn Marino /* The ports currently using this infrastructure can typically 51*e4b17023SJohn Marino handle two cases: all incoming edges have the same destination 52*e4b17023SJohn Marino block, or all incoming edges have the same source block. These 53*e4b17023SJohn Marino two members are set to the common source or destination we found, 54*e4b17023SJohn Marino or NULL if different blocks were found. If both are NULL the 55*e4b17023SJohn Marino loop can't be optimized. */ 56*e4b17023SJohn Marino basic_block incoming_src; 57*e4b17023SJohn Marino basic_block incoming_dest; 58*e4b17023SJohn Marino 59*e4b17023SJohn Marino /* First block in the loop. This is the one branched to by the loop_end 60*e4b17023SJohn Marino insn. */ 61*e4b17023SJohn Marino basic_block head; 62*e4b17023SJohn Marino 63*e4b17023SJohn Marino /* Last block in the loop (the one with the loop_end insn). */ 64*e4b17023SJohn Marino basic_block tail; 65*e4b17023SJohn Marino 66*e4b17023SJohn Marino /* The successor block of the loop. This is the one the loop_end insn 67*e4b17023SJohn Marino falls into. */ 68*e4b17023SJohn Marino basic_block successor; 69*e4b17023SJohn Marino 70*e4b17023SJohn Marino /* The last instruction in the tail. */ 71*e4b17023SJohn Marino rtx last_insn; 72*e4b17023SJohn Marino 73*e4b17023SJohn Marino /* The loop_end insn. */ 74*e4b17023SJohn Marino rtx loop_end; 75*e4b17023SJohn Marino 76*e4b17023SJohn Marino /* The iteration register. */ 77*e4b17023SJohn Marino rtx iter_reg; 78*e4b17023SJohn Marino 79*e4b17023SJohn Marino /* The new label placed at the beginning of the loop. */ 80*e4b17023SJohn Marino rtx start_label; 81*e4b17023SJohn Marino 82*e4b17023SJohn Marino /* The new label placed at the end of the loop. */ 83*e4b17023SJohn Marino rtx end_label; 84*e4b17023SJohn Marino 85*e4b17023SJohn Marino /* The length of the loop. */ 86*e4b17023SJohn Marino int length; 87*e4b17023SJohn Marino 88*e4b17023SJohn Marino /* The nesting depth of the loop. Innermost loops are given a depth 89*e4b17023SJohn Marino of 1. Only successfully optimized doloops are counted; if an inner 90*e4b17023SJohn Marino loop was marked as bad, it does not increase the depth of its parent 91*e4b17023SJohn Marino loop. 92*e4b17023SJohn Marino This value is valid when the target's optimize function is called. */ 93*e4b17023SJohn Marino int depth; 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino /* True if we can't optimize this loop. */ 96*e4b17023SJohn Marino bool bad; 97*e4b17023SJohn Marino 98*e4b17023SJohn Marino /* True if we have visited this loop during the optimization phase. */ 99*e4b17023SJohn Marino bool visited; 100*e4b17023SJohn Marino 101*e4b17023SJohn Marino /* The following values are collected before calling the target's optimize 102*e4b17023SJohn Marino function and are not valid earlier. */ 103*e4b17023SJohn Marino 104*e4b17023SJohn Marino /* Record information about control flow: whether the loop has calls 105*e4b17023SJohn Marino or asm statements, whether it has edges that jump out of the loop, 106*e4b17023SJohn Marino or edges that jump within the loop. */ 107*e4b17023SJohn Marino bool has_call; 108*e4b17023SJohn Marino bool has_asm; 109*e4b17023SJohn Marino bool jumps_within; 110*e4b17023SJohn Marino bool jumps_outof; 111*e4b17023SJohn Marino 112*e4b17023SJohn Marino /* True if there is an instruction other than the doloop_end which uses the 113*e4b17023SJohn Marino iteration register. */ 114*e4b17023SJohn Marino bool iter_reg_used; 115*e4b17023SJohn Marino /* True if the iteration register lives past the doloop instruction. */ 116*e4b17023SJohn Marino bool iter_reg_used_outside; 117*e4b17023SJohn Marino 118*e4b17023SJohn Marino /* Hard registers set at any point in the loop, except for the loop counter 119*e4b17023SJohn Marino register's set in the doloop_end instruction. */ 120*e4b17023SJohn Marino HARD_REG_SET regs_set_in_loop; 121*e4b17023SJohn Marino }; 122*e4b17023SJohn Marino 123*e4b17023SJohn Marino /* A set of hooks to be defined by a target that wants to use the reorg_loops 124*e4b17023SJohn Marino functionality. 125*e4b17023SJohn Marino 126*e4b17023SJohn Marino reorg_loops is intended to handle cases where special hardware loop 127*e4b17023SJohn Marino setup instructions are required before the loop, for example to set 128*e4b17023SJohn Marino up loop counter registers that are not exposed to the register 129*e4b17023SJohn Marino allocator, or to inform the hardware about loop bounds. 130*e4b17023SJohn Marino 131*e4b17023SJohn Marino reorg_loops performs analysis to discover loop_end patterns created 132*e4b17023SJohn Marino by the earlier loop-doloop pass, and sets up a hwloop_info 133*e4b17023SJohn Marino structure for each such insn it finds. It then tries to discover 134*e4b17023SJohn Marino the basic blocks containing the loop by tracking the lifetime of 135*e4b17023SJohn Marino the iteration register. 136*e4b17023SJohn Marino 137*e4b17023SJohn Marino If a valid loop can't be found, the FAIL function is called; 138*e4b17023SJohn Marino otherwise the OPT function is called for each loop, visiting 139*e4b17023SJohn Marino innermost loops first and ascending. */ 140*e4b17023SJohn Marino struct hw_doloop_hooks 141*e4b17023SJohn Marino { 142*e4b17023SJohn Marino /* Examine INSN. If it is a suitable doloop_end pattern, return the 143*e4b17023SJohn Marino iteration register, which should be a single hard register. 144*e4b17023SJohn Marino Otherwise, return NULL_RTX. */ 145*e4b17023SJohn Marino rtx (*end_pattern_reg) (rtx insn); 146*e4b17023SJohn Marino /* Optimize LOOP. The target should perform any additional analysis 147*e4b17023SJohn Marino (e.g. checking that the loop isn't too long), and then perform 148*e4b17023SJohn Marino its transformations. Return true if successful, false if the 149*e4b17023SJohn Marino loop should be marked bad. If it returns false, the FAIL 150*e4b17023SJohn Marino function is called. */ 151*e4b17023SJohn Marino bool (*opt) (hwloop_info loop); 152*e4b17023SJohn Marino /* Handle a loop that was marked bad for any reason. This could be 153*e4b17023SJohn Marino used to split the doloop_end pattern. */ 154*e4b17023SJohn Marino void (*fail) (hwloop_info loop); 155*e4b17023SJohn Marino }; 156*e4b17023SJohn Marino 157*e4b17023SJohn Marino extern void reorg_loops (bool, struct hw_doloop_hooks *); 158