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