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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #ifdef USE_PCH
00061 #include "opt_pch.h"
00062 #endif // USE_PCH
00063 #pragma hdrstop
00064
00065
00066 #include "defs.h"
00067 #include "cxx_memory.h"
00068 #include "opt_cfg.h"
00069 #include "opt_sym.h"
00070 #include "opt_htable.h"
00071 #include "opt_config.h"
00072 #include "tracing.h"
00073 #include "config_targ.h"
00074 #include "opt_util.h"
00075 #include "opt_sys.h"
00076 #include "opt_bb.h"
00077 #include "opt_main.h"
00078
00079
00080
00081
00082
00083
00084
00085 class OPTCOUNT {
00086 private:
00087 MEM_POOL _mpool;
00088 CFG *_cfg;
00089 OPT_STAB*_opt_stab;
00090 BOOL _tracing;
00091
00092 UINT16 *_loads;
00093 UINT16 *_stores;
00094 UINT16 *_iloads;
00095 UINT16 *_istores;
00096 UINT32 *_freq;
00097
00098 UINT32 _weighted_total_loads;
00099 UINT32 _weighted_total_stores;
00100 UINT32 _weighted_total_iloads;
00101 UINT32 _weighted_total_istores;
00102
00103 UINT32 _total_loads;
00104 UINT32 _total_stores;
00105 UINT32 _total_iloads;
00106 UINT32 _total_istores;
00107
00108 CFG *Cfg(void) const { return _cfg; }
00109 OPT_STAB *Opt_stab(void) const { return _opt_stab; }
00110
00111 UINT16 Loads(IDTYPE bb_id) const { return _loads[bb_id]; }
00112 UINT16 Stores(IDTYPE bb_id) const { return _stores[bb_id]; }
00113 UINT16 Iloads(IDTYPE bb_id) const { return _iloads[bb_id]; }
00114 UINT16 Istores(IDTYPE bb_id)const { return _istores[bb_id]; }
00115 UINT32 Freq(IDTYPE bb_id) const { return _freq[bb_id]; }
00116
00117 void Inc_loads(IDTYPE bb_id) { _loads[bb_id]++; }
00118 void Inc_stores(IDTYPE bb_id) { _stores[bb_id]++; }
00119 void Inc_iloads(IDTYPE bb_id) { _iloads[bb_id]++; }
00120 void Inc_istores(IDTYPE bb_id) { _istores[bb_id]++; }
00121 void Set_freq(IDTYPE bb_id, UINT32 c)
00122 { _freq[bb_id] = c; }
00123
00124 UINT32 Total_loads(void) { return _total_loads; }
00125 UINT32 Total_stores(void) { return _total_stores; }
00126 UINT32 Total_iloads(void) { return _total_iloads; }
00127 UINT32 Total_istores(void) { return _total_istores; }
00128
00129 void Acc_total_loads(UINT32 x) { _total_loads += x; }
00130 void Acc_total_stores(UINT32 x) { _total_stores += x; }
00131 void Acc_total_iloads(UINT32 x) { _total_iloads += x; }
00132 void Acc_total_istores(UINT32 x){ _total_istores += x; }
00133
00134 UINT32 Weighted_total_loads(void) { return _weighted_total_loads; }
00135 UINT32 Weighted_total_stores(void){ return _weighted_total_stores; }
00136 UINT32 Weighted_total_iloads(void){ return _weighted_total_iloads; }
00137 UINT32 Weighted_total_istores(void){ return _weighted_total_istores; }
00138
00139 void Acc_weighted_total_loads(UINT32 x) { _weighted_total_loads += x; }
00140 void Acc_weighted_total_stores(UINT32 x) { _weighted_total_stores += x; }
00141 void Acc_weighted_total_iloads(UINT32 x) { _weighted_total_iloads += x; }
00142 void Acc_weighted_total_istores(UINT32 x){ _weighted_total_istores += x;}
00143
00144 public:
00145 OPTCOUNT(CFG *cfg, OPT_STAB *opt_stab): _cfg(cfg), _opt_stab(opt_stab)
00146 {
00147 OPT_POOL_Initialize(&_mpool, "opt_count_mempool", FALSE, -1);
00148 OPT_POOL_Push(&_mpool, -1);
00149 _tracing = Get_Trace( TP_GLOBOPT, STATISTICS_FLAG);
00150 _loads = (UINT16 *) CXX_NEW_ARRAY(UINT16, Cfg()->Total_bb_count(), &_mpool);
00151 _stores = (UINT16 *) CXX_NEW_ARRAY(UINT16, Cfg()->Total_bb_count(), &_mpool);
00152 _iloads = (UINT16 *) CXX_NEW_ARRAY(UINT16, Cfg()->Total_bb_count(), &_mpool);
00153 _istores = (UINT16 *) CXX_NEW_ARRAY(UINT16, Cfg()->Total_bb_count(), &_mpool);
00154 _freq = (UINT32 *) CXX_NEW_ARRAY(UINT32, Cfg()->Total_bb_count(), &_mpool);
00155 BZERO(_loads, Cfg()->Total_bb_count()*sizeof(_loads[0]));
00156 BZERO(_stores, Cfg()->Total_bb_count()*sizeof(_stores[0]));
00157 BZERO(_iloads, Cfg()->Total_bb_count()*sizeof(_iloads[0]));
00158 BZERO(_istores, Cfg()->Total_bb_count()*sizeof(_istores[0]));
00159 BZERO(_freq, Cfg()->Total_bb_count()*sizeof(_freq[0]));
00160
00161 _weighted_total_loads = _weighted_total_stores =
00162 _weighted_total_iloads = _weighted_total_istores = 0;
00163
00164 _total_loads = _total_stores = _total_iloads = _total_istores = 0;
00165
00166 }
00167
00168 ~OPTCOUNT(void)
00169 {
00170 OPT_POOL_Pop(&_mpool, -1);
00171 OPT_POOL_Delete(&_mpool, -1);
00172 }
00173 void Bottom_up_cr(IDTYPE bb, CODEREP *cr, BOOL is_store = FALSE);
00174 void Bottom_up_stmt(STMTREP *stmt, IDTYPE bb);
00175
00176
00177 void Collect_statistics( void );
00178
00179 BOOL Tracing(void) const { return _tracing; }
00180 };
00181
00182 void
00183 OPTCOUNT::Bottom_up_cr(IDTYPE bb, CODEREP *cr, BOOL is_store)
00184 {
00185 switch (cr->Kind()) {
00186 case CK_CONST:
00187 case CK_RCONST:
00188 case CK_LDA:
00189 break;
00190
00191 case CK_VAR:
00192
00193 if (ST_class( Opt_stab()->St(cr->Aux_id()) ) != CLASS_PREG)
00194 Inc_loads(bb);
00195 break;
00196
00197 case CK_IVAR:
00198
00199
00200 if (cr->Opr() == OPR_PARM) {
00201 if (cr->Offset() & WN_PARM_DUMMY)
00202 break;
00203 }
00204 else {
00205 if (is_store)
00206 Inc_istores(bb);
00207 else
00208 Inc_iloads(bb);
00209 }
00210
00211 Bottom_up_cr(bb, is_store ? cr->Istr_base() : cr->Ilod_base());
00212
00213 if ( cr->Opr() == OPR_MLOAD ) {
00214 Bottom_up_cr(bb, cr->Mload_size() ? cr->Mload_size() : cr->Mstore_size());
00215 }
00216 else if ( cr->Opr() == OPR_ILOADX ) {
00217 Bottom_up_cr(bb, cr->Index());
00218 }
00219 break;
00220
00221 case CK_OP:
00222 {
00223 for (INT32 i=0; i<cr->Kid_count(); i++) {
00224 Bottom_up_cr(bb, cr->Opnd(i));
00225 }
00226 break;
00227 }
00228 default:
00229 break;
00230 }
00231 }
00232
00233 void
00234 OPTCOUNT::Bottom_up_stmt(STMTREP *stmt, IDTYPE bb)
00235 {
00236 const OPERATOR stmt_opr = stmt->Opr();
00237
00238 CODEREP *rhs = stmt->Rhs();
00239
00240 if (OPCODE_is_call(stmt->Op())) {
00241 for (INT32 i = 0; i < rhs->Kid_count(); i++) {
00242 Bottom_up_cr(bb, rhs->Opnd(i));
00243 }
00244 } else if (rhs != NULL) {
00245 Bottom_up_cr(bb, rhs);
00246 }
00247
00248 if (OPERATOR_is_scalar_istore (stmt_opr)) {
00249 Bottom_up_cr(bb, stmt->Lhs(), TRUE);
00250 } else if (OPERATOR_is_scalar_store (stmt_opr)) {
00251
00252 switch(stmt->Lhs()->Kind()) {
00253 case CK_VAR:
00254 if (ST_class(Opt_stab()->St(stmt->Lhs()->Aux_id())) != CLASS_PREG)
00255 Inc_stores(bb);
00256 break;
00257 default:
00258 Inc_stores(bb);
00259 Bottom_up_cr(bb,stmt->Lhs());
00260 }
00261 }
00262
00263 }
00264
00265 void
00266 OPTCOUNT::Collect_statistics( void )
00267 {
00268 BB_NODE *bb;
00269 CFG_ITER cfg_iter(Cfg());
00270
00271 FOR_ALL_ELEM (bb, cfg_iter, Init()) {
00272 Is_Trace(Tracing(),(TFile,
00273 "====== OPTCOUNT::Collect_static_statistics, BB%d ======\n",
00274 bb->Id()));
00275
00276 Set_freq(bb->Id(), bb->Freq());
00277
00278 STMTREP_ITER stmt_iter(bb->Stmtlist());
00279 STMTREP *stmt;
00280 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
00281 Is_Trace_cmd(Tracing(),stmt->Print(TFile));
00282 Bottom_up_stmt(stmt, bb->Id());
00283 }
00284
00285 Is_Trace(Tracing(),(TFile,
00286 "Loads %d, Stores %d, Iloads %d, Istores %d, Freq %d \n",
00287 Loads(bb->Id()), Stores(bb->Id()), Iloads(bb->Id()),
00288 Istores(bb->Id()), Freq(bb->Id())));
00289
00290 }
00291
00292 for (UINT i = 0; i < Cfg()->Total_bb_count(); ++i) {
00293 Acc_total_loads(Loads(i));
00294 Acc_total_stores(Stores(i));
00295 Acc_total_iloads(Iloads(i));
00296 Acc_total_istores(Istores(i));
00297
00298 Acc_weighted_total_loads(Loads(i)*Freq(i));
00299 Acc_weighted_total_stores(Stores(i)*Freq(i));
00300 Acc_weighted_total_iloads(Iloads(i)*Freq(i));
00301 Acc_weighted_total_istores(Istores(i)*Freq(i));
00302 }
00303
00304 fprintf(TFile,"Total static: loads %12d, stores %12d, iloads %12d, istores %12d\n", Total_loads(), Total_stores(), Total_iloads(), Total_istores());
00305 fprintf(TFile,"Total dynamic : loads %12d, stores %12d, iloads %12d, istores %12d\n", Weighted_total_loads(), Weighted_total_stores(), Weighted_total_iloads(), Weighted_total_istores());
00306 }
00307
00308 void
00309 COMP_UNIT::Collect_statistics(void)
00310 {
00311 OPTCOUNT optcount( Cfg(), Opt_stab() );
00312
00313 optcount.Collect_statistics();
00314
00315 }
00316
00317
00318
00319
00320
00321
00322