00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "profile_util.h"
00043 #include "freq.h"
00044
00045 #define NONE_EDGE -1.00
00046 #define ZERO_FREQ 0.0
00047 #define ZERO_PROB 0.0
00048 #define FREQ_CHECK_MSG_MAX 10
00049
00050 BBLIST* Find_Edge(BB* src_bb,BB* tgt_bb)
00051 {
00052 BBLIST* succ_edge;
00053 for(succ_edge = BB_succs(src_bb); succ_edge != NULL;
00054 succ_edge = BBLIST_next(succ_edge))
00055 {
00056 if(BBLIST_item(succ_edge) == tgt_bb)
00057 return succ_edge;
00058 }
00059 return succ_edge;
00060 }
00061
00062
00063 float Freq(BB* src_bb,BB* tgt_bb)
00064 {
00065 BBLIST* succ_edge;
00066 for(succ_edge = BB_succs(src_bb); succ_edge != NULL;
00067 succ_edge = BBLIST_next(succ_edge))
00068 {
00069 if(BBLIST_item(succ_edge) == tgt_bb)
00070 return BBLIST_freq(succ_edge);
00071 }
00072 if(succ_edge == NULL)
00073 return NONE_EDGE;
00074 }
00075
00076
00077 float Prob_Local(BB* src_bb, BB* tgt_bb)
00078 {
00079 BBLIST* edge = Find_Edge(src_bb,tgt_bb);
00080 if ( edge != NULL )
00081 return BBLIST_prob(edge);
00082 else
00083 return 0;
00084 }
00085
00086 BOOL Set_Freq( BB* src_bb, BB* tgt_bb, float nfreq)
00087 {
00088 BBLIST* succ_edge;
00089 for(succ_edge = BB_succs(src_bb); succ_edge != NULL;
00090 succ_edge=BBLIST_next(succ_edge))
00091 {
00092 if(BBLIST_item(succ_edge) == tgt_bb)
00093 {
00094 BBLIST_freq(succ_edge) = nfreq;
00095 return TRUE;
00096 }
00097 }
00098 if(succ_edge==NULL)
00099 return FALSE;
00100 }
00101
00102 BOOL Set_Prob( BB* src_bb, BB* tgt_bb, float nprob)
00103 {
00104 BBLIST* succ_edge;
00105 for(succ_edge=BB_succs(src_bb); succ_edge!=NULL;
00106 succ_edge=BBLIST_next(succ_edge))
00107 {
00108 if(BBLIST_item(succ_edge) == tgt_bb)
00109 {
00110 Is_True(REGION_First_BB != NULL, ("Region first BB can not be NULL"));
00111 Is_True(nprob <= 1.0,("prob can not excess 1.0 "));
00112 Is_True(nprob >= ZERO_PROB, ("prob can not be less than 0.0"));
00113 BBLIST_prob(succ_edge) = nprob;
00114 return TRUE;
00115 }
00116 }
00117 if(succ_edge==NULL)
00118 return FALSE;
00119 }
00120
00121 BB* Find_Last_BB(BB* src_bb)
00122 {
00123 BB* lbb;
00124 for(lbb = src_bb; BB_next(lbb) != NULL; lbb = BB_next(lbb))
00125 ;
00126 return lbb;
00127 }
00128
00129 BOOL Prob_OK(BB* bb)
00130 {
00131 if (!BB_succs(bb)) return TRUE;
00132 float prob = 0.0F;
00133 for(BBLIST* edge = BB_succs(bb); edge != NULL;
00134 edge = BBLIST_next(edge))
00135 {
00136 prob += BBLIST_prob(edge);
00137 }
00138 return FREQ_Match(prob, 1.0F);
00139 }
00140
00141 BOOL Freq_OK(BB* bb)
00142 {
00143 if (!BB_succs(bb)) return TRUE;
00144 float prob = 0.0F;
00145 float edge_freq, in_total_freq, out_total_freq;
00146 edge_freq = in_total_freq = out_total_freq = 0.0F;
00147
00148 if(BB_succs(bb))
00149 {
00150
00151
00152 for(BBLIST* edge = BB_succs(bb); edge != NULL;
00153 edge = BBLIST_next(edge))
00154 {
00155 prob = BBLIST_prob(edge);
00156 edge_freq = prob * BB_freq(bb);
00157 if(!FREQ_Match(edge_freq, BBLIST_freq(edge)))
00158 {
00159 return FALSE;
00160 }
00161 out_total_freq += edge_freq;
00162 }
00163 }
00164
00165
00166 for(BBLIST* edge = BB_preds(bb); edge != NULL;
00167 edge = BBLIST_next(edge))
00168 {
00169 prob = Prob_Local(BBLIST_item(edge),bb);
00170 edge_freq = prob * BB_freq(BBLIST_item(edge));
00171 in_total_freq += edge_freq;
00172 }
00173
00174 if(BB_succs(bb) && BB_preds(bb))
00175 {
00176 if(!FREQ_Match(in_total_freq,out_total_freq))
00177 {
00178 return FALSE;
00179 }
00180 }
00181
00182
00183 if(BB_succs(bb))
00184 {
00185 if(!FREQ_Match(BB_freq(bb),out_total_freq))
00186 return FALSE;
00187 }
00188 return TRUE;
00189 }
00190
00191 BOOL Check_CFG_Consistency(const char* caller)
00192 {
00193 INT msg1_cnt = 0;
00194 INT msg2_cnt = 0;
00195 BOOL is_ok = TRUE;
00196 BB *bb;
00197
00198 for (bb = REGION_First_BB; bb != NULL; bb = BB_next(bb)) {
00199 if (!Prob_OK(bb)) {
00200 is_ok = FALSE;
00201 ++msg1_cnt;
00202 if (msg1_cnt <= FREQ_CHECK_MSG_MAX) {
00203 DevWarn("%s: Succ probs of BB:%d do not sum to 1.0", caller,
00204 BB_id(bb));
00205 } else if (msg1_cnt == FREQ_CHECK_MSG_MAX+1) {
00206 DevWarn("%s: more than %d Succ probs sum msgs - disabling msg\n",
00207 caller, FREQ_CHECK_MSG_MAX);
00208 }
00209 }
00210
00211 if (!Freq_OK(bb)) {
00212 is_ok = FALSE;
00213 ++msg2_cnt;
00214 if (msg2_cnt <= FREQ_CHECK_MSG_MAX) {
00215 DevWarn("%s: BB:%d freq (%g) != the sum of its incoming edge freq",
00216 caller, BB_id(bb), BB_freq(bb));
00217 } else if (msg2_cnt == FREQ_CHECK_MSG_MAX+1) {
00218 DevWarn("%s: more than %d sum of pred msgs - disabling msg\n",
00219 caller, FREQ_CHECK_MSG_MAX);
00220 }
00221 }
00222 }
00223 return is_ok;
00224 }
00225
00226 BOOL Check_BB_Consistency(const char* caller,BB* bb,INT msg1_cnt,INT msg2_cnt)
00227 {
00228 bool is_ok = TRUE;
00229 if (!Prob_OK(bb))
00230 {
00231 is_ok = FALSE;
00232 ++msg1_cnt;
00233 if (msg1_cnt <= FREQ_CHECK_MSG_MAX)
00234 {
00235 DevWarn("%s: Succ probs of BB:%d do not sum to 1.0", caller,
00236 BB_id(bb));
00237 } else if (msg1_cnt == FREQ_CHECK_MSG_MAX+1)
00238 {
00239 DevWarn("%s: more than %d Succ probs sum msgs - disabling msg\n",
00240 caller, FREQ_CHECK_MSG_MAX);
00241 }
00242 }
00243
00244 if (!Freq_OK(bb))
00245 {
00246 is_ok = FALSE;
00247 ++msg2_cnt;
00248 if (msg2_cnt <= FREQ_CHECK_MSG_MAX) {
00249 DevWarn("%s: BB:%d freq (%g) != the sum of its pred edge freq",
00250 caller, BB_id(bb), BB_freq(bb));
00251 } else if (msg2_cnt == FREQ_CHECK_MSG_MAX+1)
00252 {
00253 DevWarn("%s: more than %d sum of pred msgs - disabling msg\n",
00254 caller, FREQ_CHECK_MSG_MAX);
00255 }
00256 }
00257 }
00258
00259 float BB_Total_In_Freq(BB *bb)
00260 {
00261 float total_in = 0.0F;
00262 for(BBLIST* in_edge = BB_preds(bb); in_edge != NULL;
00263 in_edge = BBLIST_next(in_edge))
00264 total_in += Freq(BBLIST_item(in_edge),bb);
00265 return total_in;
00266 }
00267
00268 float BB_Total_Out_Freq(BB *bb)
00269 {
00270 float total_out = 0.0F;
00271 for(BBLIST* out_edge = BB_succs(bb); out_edge != NULL;
00272 out_edge = BBLIST_next(out_edge))
00273 total_out += Freq(bb,BBLIST_item(out_edge));
00274 return total_out;
00275 }
00276
00277 LABEL_IDX Get_Op_Label(op* branch_op)
00278 {
00279 INT tn_num = OP_opnds( branch_op );
00280 for( INT i = 0; i<tn_num; i++)
00281 {
00282 TN* tgt_tn = OP_opnd( branch_op, i );
00283 if( TN_is_label(tgt_tn) )
00284 return TN_label(tgt_tn);
00285 }
00286 }
00287
00288 BOOL TN_In_BB(BB* bb, TN* tn)
00289 {
00290 OP* opcode = bb->ops.first;
00291 for( ; opcode != NULL; opcode = OP_next(opcode))
00292 {
00293 INT i;
00294 for (i = 0; i < OP_results(opcode); i++)
00295 {
00296 if (tn == OP_result(opcode,i))
00297 return TRUE;
00298 }
00299
00300 for (i = 0; i < OP_opnds(opcode); i++)
00301 {
00302 if (tn == OP_opnd(opcode,i))
00303 return TRUE;
00304 }
00305 }
00306 return FALSE;
00307 }