1*e4b17023SJohn Marino/* This file contains the list of the debug counter for GCC. 2*e4b17023SJohn Marino Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc. 3*e4b17023SJohn Marino 4*e4b17023SJohn MarinoThis file is part of GCC. 5*e4b17023SJohn Marino 6*e4b17023SJohn MarinoGCC is free software; you can redistribute it and/or modify it under 7*e4b17023SJohn Marinothe terms of the GNU General Public License as published by the Free 8*e4b17023SJohn MarinoSoftware Foundation; either version 3, or (at your option) any later 9*e4b17023SJohn Marinoversion. 10*e4b17023SJohn Marino 11*e4b17023SJohn MarinoGCC is distributed in the hope that it will be useful, but WITHOUT ANY 12*e4b17023SJohn MarinoWARRANTY; without even the implied warranty of MERCHANTABILITY or 13*e4b17023SJohn MarinoFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14*e4b17023SJohn Marinofor more details. 15*e4b17023SJohn Marino 16*e4b17023SJohn MarinoYou should have received a copy of the GNU General Public License 17*e4b17023SJohn Marinoalong with GCC; see the file COPYING3. If not see 18*e4b17023SJohn Marino<http://www.gnu.org/licenses/>. */ 19*e4b17023SJohn Marino 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino/* A debug counter provides you a way to count an event 22*e4b17023SJohn Marino and return false after the counter has exceeded the threshold 23*e4b17023SJohn Marino specified by the option. 24*e4b17023SJohn Marino 25*e4b17023SJohn Marino What is it used for ? 26*e4b17023SJohn Marino 27*e4b17023SJohn Marino This is primarily used to speed up the search for the bad transformation 28*e4b17023SJohn Marino an optimization pass does. By doing a binary search on N, 29*e4b17023SJohn Marino you can quickly narrow down to one transformation 30*e4b17023SJohn Marino which is bad, or which triggers the bad behavior downstream 31*e4b17023SJohn Marino (usually in the form of the badly generated code). 32*e4b17023SJohn Marino 33*e4b17023SJohn Marino How does it work ? 34*e4b17023SJohn Marino 35*e4b17023SJohn Marino Every time dbg_cnt(named-counter) is called, 36*e4b17023SJohn Marino the counter is incremented for the named-counter. 37*e4b17023SJohn Marino And the incremented value is compared against the threshold (limit) 38*e4b17023SJohn Marino specified by the option. 39*e4b17023SJohn Marino dbg_cnt () returns true if it is at or below threshold, and false if above. 40*e4b17023SJohn Marino 41*e4b17023SJohn Marino How to add a new one ? 42*e4b17023SJohn Marino 43*e4b17023SJohn Marino To add a new counter, simply add an entry below with some descriptive name, 44*e4b17023SJohn Marino and add call(s) to dbg_cnt(your-counter-name) in appropriate places. 45*e4b17023SJohn Marino Usually, you want to control at the finest granularity 46*e4b17023SJohn Marino any particular transformation can happen. 47*e4b17023SJohn Marino e.g. for each instruction in a dead code elimination, 48*e4b17023SJohn Marino or for each copy instruction in register coalescing, 49*e4b17023SJohn Marino or constant-propagation for each insn, 50*e4b17023SJohn Marino or a block straightening, etc. 51*e4b17023SJohn Marino See dce.c for an example. With the dbg_cnt () call in dce.c, 52*e4b17023SJohn Marino now a developer can use -fdbg-cnt=dce:N 53*e4b17023SJohn Marino to stop doing the dead code elimination after N times. 54*e4b17023SJohn Marino 55*e4b17023SJohn Marino How to use it ? 56*e4b17023SJohn Marino 57*e4b17023SJohn Marino By default, all limits are UINT_MAX. 58*e4b17023SJohn Marino Since debug count is unsigned int, <= UINT_MAX returns true always. 59*e4b17023SJohn Marino i.e. dbg_cnt() returns true always regardless of the counter value 60*e4b17023SJohn Marino (although it still counts the event). 61*e4b17023SJohn Marino Use -fdbg-cnt=counter1:N,counter2:M,... 62*e4b17023SJohn Marino which sets the limit for counter1 to N, and the limit for counter2 to M, etc. 63*e4b17023SJohn Marino e.g. setting a limit to zero will make dbg_cnt () return false *always*. 64*e4b17023SJohn Marino 65*e4b17023SJohn Marino The following shell file can then be used to binary search for 66*e4b17023SJohn Marino exact transformation that causes the bug. A second shell script 67*e4b17023SJohn Marino should be written, say "tryTest", which exits with 1 if the 68*e4b17023SJohn Marino compiled program fails and exits with 0 if the program succeeds. 69*e4b17023SJohn Marino This shell script should take 1 parameter, the value to be passed 70*e4b17023SJohn Marino to set the counter of the compilation command in tryTest. Then, 71*e4b17023SJohn Marino assuming that the following script is called binarySearch, 72*e4b17023SJohn Marino the command: 73*e4b17023SJohn Marino 74*e4b17023SJohn Marino binarySearch tryTest 75*e4b17023SJohn Marino 76*e4b17023SJohn Marino will automatically find the highest value of the counter for which 77*e4b17023SJohn Marino the program fails. If tryTest never fails, binarySearch will 78*e4b17023SJohn Marino produce unpredictable results as it will try to find an upper bound 79*e4b17023SJohn Marino that does not exist. 80*e4b17023SJohn Marino 81*e4b17023SJohn Marino When dbgcnt does hits the limit, it writes a comment in the current 82*e4b17023SJohn Marino dump_file of the form: 83*e4b17023SJohn Marino 84*e4b17023SJohn Marino ***dbgcnt: limit reached for %s.*** 85*e4b17023SJohn Marino 86*e4b17023SJohn Marino Assuming that the dump file is logging the analysis/transformations 87*e4b17023SJohn Marino it is making, this pinpoints the exact position in the log file 88*e4b17023SJohn Marino where the problem transformation is being logged. 89*e4b17023SJohn Marino 90*e4b17023SJohn Marino===================================== 91*e4b17023SJohn Marino#!/bin/bash 92*e4b17023SJohn Marino 93*e4b17023SJohn Marinowhile getopts "l:u:i:" opt 94*e4b17023SJohn Marinodo 95*e4b17023SJohn Marino case $opt in 96*e4b17023SJohn Marino l) lb="$OPTARG";; 97*e4b17023SJohn Marino u) ub="$OPTARG";; 98*e4b17023SJohn Marino i) init="$OPTARG";; 99*e4b17023SJohn Marino ?) usage; exit 3;; 100*e4b17023SJohn Marino esac 101*e4b17023SJohn Marinodone 102*e4b17023SJohn Marino 103*e4b17023SJohn Marinoshift $(($OPTIND - 1)) 104*e4b17023SJohn Marinoecho $@ 105*e4b17023SJohn Marinocmd=${1+"${@}"} 106*e4b17023SJohn Marino 107*e4b17023SJohn Marinolb=${lb:=0} 108*e4b17023SJohn Marinoinit=${init:=100} 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino$cmd $lb 111*e4b17023SJohn Marinolb_val=$? 112*e4b17023SJohn Marinoif [ -z "$ub" ]; then 113*e4b17023SJohn Marino # find the upper bound 114*e4b17023SJohn Marino ub=$(($init + $lb)) 115*e4b17023SJohn Marino true 116*e4b17023SJohn Marino while [ $? -eq $lb_val ]; do 117*e4b17023SJohn Marino ub=$(($ub * 10)) 118*e4b17023SJohn Marino #ub=`expr $ub \* 10` 119*e4b17023SJohn Marino $cmd $ub 120*e4b17023SJohn Marino done 121*e4b17023SJohn Marinofi 122*e4b17023SJohn Marino 123*e4b17023SJohn Marinoecho command: $cmd 124*e4b17023SJohn Marino 125*e4b17023SJohn Marinotrue 126*e4b17023SJohn Marinowhile [ `expr $ub - $lb` -gt 1 ]; do 127*e4b17023SJohn Marino try=$(($lb + ( $ub - $lb ) / 2)) 128*e4b17023SJohn Marino $cmd $try 129*e4b17023SJohn Marino if [ $? -eq $lb_val ]; then 130*e4b17023SJohn Marino lb=$try 131*e4b17023SJohn Marino else 132*e4b17023SJohn Marino ub=$try 133*e4b17023SJohn Marino fi 134*e4b17023SJohn Marinodone 135*e4b17023SJohn Marino 136*e4b17023SJohn Marinoecho lbound: $lb 137*e4b17023SJohn Marinoecho ubound: $ub 138*e4b17023SJohn Marino 139*e4b17023SJohn Marino===================================== 140*e4b17023SJohn Marino 141*e4b17023SJohn Marino*/ 142*e4b17023SJohn Marino 143*e4b17023SJohn Marino/* Debug counter definitions. */ 144*e4b17023SJohn MarinoDEBUG_COUNTER (auto_inc_dec) 145*e4b17023SJohn MarinoDEBUG_COUNTER (ccp) 146*e4b17023SJohn MarinoDEBUG_COUNTER (cfg_cleanup) 147*e4b17023SJohn MarinoDEBUG_COUNTER (cse2_move2add) 148*e4b17023SJohn MarinoDEBUG_COUNTER (cprop) 149*e4b17023SJohn MarinoDEBUG_COUNTER (dce) 150*e4b17023SJohn MarinoDEBUG_COUNTER (dce_fast) 151*e4b17023SJohn MarinoDEBUG_COUNTER (dce_ud) 152*e4b17023SJohn MarinoDEBUG_COUNTER (delete_trivial_dead) 153*e4b17023SJohn MarinoDEBUG_COUNTER (df_byte_scan) 154*e4b17023SJohn MarinoDEBUG_COUNTER (dse) 155*e4b17023SJohn MarinoDEBUG_COUNTER (dse1) 156*e4b17023SJohn MarinoDEBUG_COUNTER (dse2) 157*e4b17023SJohn MarinoDEBUG_COUNTER (gcse2_delete) 158*e4b17023SJohn MarinoDEBUG_COUNTER (global_alloc_at_func) 159*e4b17023SJohn MarinoDEBUG_COUNTER (global_alloc_at_reg) 160*e4b17023SJohn MarinoDEBUG_COUNTER (graphite_scop) 161*e4b17023SJohn MarinoDEBUG_COUNTER (hoist) 162*e4b17023SJohn MarinoDEBUG_COUNTER (hoist_insn) 163*e4b17023SJohn MarinoDEBUG_COUNTER (ia64_sched2) 164*e4b17023SJohn MarinoDEBUG_COUNTER (if_conversion) 165*e4b17023SJohn MarinoDEBUG_COUNTER (if_conversion_tree) 166*e4b17023SJohn MarinoDEBUG_COUNTER (if_after_combine) 167*e4b17023SJohn MarinoDEBUG_COUNTER (if_after_reload) 168*e4b17023SJohn MarinoDEBUG_COUNTER (local_alloc_for_sched) 169*e4b17023SJohn MarinoDEBUG_COUNTER (postreload_cse) 170*e4b17023SJohn MarinoDEBUG_COUNTER (pre) 171*e4b17023SJohn MarinoDEBUG_COUNTER (pre_insn) 172*e4b17023SJohn MarinoDEBUG_COUNTER (treepre_insert) 173*e4b17023SJohn MarinoDEBUG_COUNTER (tree_sra) 174*e4b17023SJohn MarinoDEBUG_COUNTER (eipa_sra) 175*e4b17023SJohn MarinoDEBUG_COUNTER (sched2_func) 176*e4b17023SJohn MarinoDEBUG_COUNTER (sched_block) 177*e4b17023SJohn MarinoDEBUG_COUNTER (sched_func) 178*e4b17023SJohn MarinoDEBUG_COUNTER (sched_insn) 179*e4b17023SJohn MarinoDEBUG_COUNTER (sched_region) 180*e4b17023SJohn MarinoDEBUG_COUNTER (sel_sched_cnt) 181*e4b17023SJohn MarinoDEBUG_COUNTER (sel_sched_region_cnt) 182*e4b17023SJohn MarinoDEBUG_COUNTER (sel_sched_insn_cnt) 183*e4b17023SJohn MarinoDEBUG_COUNTER (sms_sched_loop) 184*e4b17023SJohn MarinoDEBUG_COUNTER (store_motion) 185*e4b17023SJohn MarinoDEBUG_COUNTER (split_for_sched2) 186*e4b17023SJohn MarinoDEBUG_COUNTER (tail_call) 187