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 "tracing.h"
00068 #include "opt_defs.h"
00069 #include "config_wopt.h"
00070 #include "opt_cfg.h"
00071 #include "opt_mu_chi.h"
00072 #include "opt_htable.h"
00073 #include "opt_etable.h"
00074 #include "opt_estr.h"
00075
00076
00077 class HOIST_SUMMARY
00078 {
00079 private:
00080
00081 BB_NODE *_bb;
00082 UINT _succ_mask;
00083
00084 HOIST_SUMMARY *_cd_hs;
00085 UINT _which_cd_succ;
00086
00087
00088 UINT _injured:1;
00089 EXP_ID _exp_id:31;
00090 HOIST_SUMMARY *_next;
00091 UINT _succ_count;
00092 EXP_OCCURS *_exp_occur;
00093 EXP_OCCURS *_hoisted_occur;
00094
00095 public:
00096 void Init(void) {
00097 _exp_id = ETABLE::ILLEGAL_E_NUMBER;
00098 _cd_hs = NULL;
00099 _bb = NULL;
00100 _succ_mask = 0;
00101 }
00102
00103 void Init_per_expr(EXP_ID id) {
00104 _exp_id = id;
00105 _injured = 0;
00106 _succ_count = _succ_mask;
00107 _exp_occur = NULL;
00108 _hoisted_occur = NULL;
00109 _next = NULL;
00110 }
00111
00112 void Init_cfg_info(BB_NODE *bb, HOIST_SUMMARY *, ETABLE *);
00113
00114 EXP_ID Exp_id(void) const { return _exp_id; }
00115 BOOL Injured(void) const { return (_injured != 0); }
00116 BB_NODE *Bb(void) const { return _bb; }
00117 BOOL Anticipated(void) { return _succ_count == 0; }
00118 HOIST_SUMMARY *Next(void) const { return _next; }
00119 HOIST_SUMMARY *Cd_hs(void) const { return _cd_hs; }
00120 EXP_OCCURS *Exp_occur(void) const { return _exp_occur; }
00121 EXP_OCCURS *Hoisted_occur(void) const { return _hoisted_occur; }
00122 UINT Which_cd_succ(void) const { return _which_cd_succ; }
00123
00124 void Set_bb(BB_NODE *bb) { _bb = bb; }
00125 void Set_injured(void) { _injured = 1; }
00126 void Set_next(HOIST_SUMMARY *n) { _next = n; }
00127 void Set_exp_occur(EXP_OCCURS *occ) { _exp_occur = occ; }
00128 void Set_hoisted_occur(EXP_OCCURS *occ) { _hoisted_occur = occ; }
00129 void Update_succ_count(INT i) { _succ_count &= ~(1 << i); }
00130
00131
00132 BOOL Succ_processed(INT i) { return ((_succ_count & (1 << i)) == 0); }
00133
00134
00135 BOOL Anticipated_at_CD(CODEREP *) const;
00136
00137
00138 inline BOOL Def_occur_allows_hoisting(EXP_OCCURS *) const;
00139
00140 void Print(FILE *fp) const;
00141 };
00142
00143
00144 class EXP_HOISTING {
00145 private:
00146
00147 ETABLE *_etable;
00148 EXP_WORKLST *_exp_worklst;
00149 CFG *_cfg;
00150 MEM_POOL *_pool;
00151 HOIST_SUMMARY *_summary;
00152
00153
00154 EXP_ID _cur_exp_id;
00155 HOIST_SUMMARY *_head;
00156 HOIST_SUMMARY *_tail;
00157
00158 CFG *Cfg(void) const { return _cfg; }
00159 ETABLE *Etable(void) const { return _etable; }
00160 EXP_WORKLST *Exp_worklst(void) const { return _exp_worklst; }
00161 EXP_ID Cur_exp_id(void) const { return _cur_exp_id; }
00162 HOIST_SUMMARY *Summary(IDTYPE bb_id) const { return &_summary[bb_id]; }
00163
00164 void Set_cur_exp_id(EXP_ID id) { _cur_exp_id = id; }
00165
00166 void Clear_hoist_worklist(void) { _head = _tail = NULL; }
00167
00168 void Append_hoist_worklist(HOIST_SUMMARY *hs) {
00169 hs->Set_next(NULL);
00170 if (_head == NULL) {
00171 _head = _tail = hs;
00172 } else {
00173 _tail->Set_next(hs);
00174 _tail = hs;
00175 }
00176 }
00177
00178 public:
00179
00180 EXP_HOISTING(ETABLE *etable, MEM_POOL *pool)
00181 {
00182 _etable = etable;
00183 _cfg = etable->Cfg();
00184 _pool = pool;
00185 _summary = (HOIST_SUMMARY*)
00186 CXX_NEW_ARRAY(HOIST_SUMMARY, Cfg()->Total_bb_count(), _pool);
00187
00188 CFG_ITER cfg_iter;
00189 BB_NODE *bb;
00190
00191 FOR_ALL_ELEM (bb, cfg_iter, Init(Cfg())) {
00192 HOIST_SUMMARY *hs = Summary(bb->Id());
00193 hs->Init();
00194 hs->Init_cfg_info(bb, _summary, etable);
00195 }
00196
00197 if (etable->Tracing()) {
00198 FOR_ALL_ELEM (bb, cfg_iter, Init(Cfg())) {
00199 HOIST_SUMMARY *hs = Summary(bb->Id());
00200 Is_Trace(hs->Cd_hs() != NULL,
00201 (TFile, "HOISTING: BB%d is control dependent on BB%d succ-edge %d.\n",
00202 hs->Bb()->Id(), hs->Cd_hs()->Bb()->Id(), hs->Which_cd_succ()));
00203 }
00204 }
00205 }
00206
00207
00208 ~EXP_HOISTING(void)
00209 {
00210
00211
00212 }
00213
00214 MEM_POOL *Pool(void) const { return _pool; }
00215
00216 inline void Update_succ_count_rec(HOIST_SUMMARY *, UINT, CODEREP *, EXP_OCCURS *);
00217 EXP_OCCURS *Get_hoisted_exp_occur(HOIST_SUMMARY *, CODEREP *, EXP_OCCURS *, BOOL);
00218 void Generate_hoisted_occur(EXP_WORKLST *);
00219
00220 };
00221
00222 void
00223 HOIST_SUMMARY::Print(FILE *fp) const
00224 {
00225 (void) fprintf(fp, "HS> BB%d; CD on BB%d; Exp %d;\n",
00226 Bb()->Id(), (Cd_hs() ? (INT) Cd_hs()->Bb()->Id() : -1),
00227 Exp_id());
00228 (void) fprintf(fp, " ");
00229 if (Exp_occur() == NULL) {
00230 (void) fprintf(fp, " <null>\n");
00231 }
00232 else {
00233 Exp_occur()->Print(TFile);
00234 }
00235 (void) fprintf(fp, " ");
00236 if (Hoisted_occur() == NULL) {
00237 (void) fprintf(fp, " <null>\n");
00238 }
00239 else {
00240 Hoisted_occur()->Print(TFile);
00241 }
00242 }
00243
00244
00245
00246
00247
00248 void
00249 HOIST_SUMMARY::Init_cfg_info(BB_NODE *bb, HOIST_SUMMARY *summary, ETABLE *etable)
00250 {
00251 _bb = bb;
00252 _succ_mask = (1 << bb->Succ()->Len()) - 1;
00253 _cd_hs = NULL;
00254
00255 BB_NODE *temp_bb;
00256 BB_NODE_SET_ITER rcfg_iter;
00257
00258
00259
00260
00261
00262 BB_NODE *cd_bb = NULL;
00263 FOR_ALL_ELEM( temp_bb, rcfg_iter, Init( bb->Rcfg_dom_frontier() ) ) {
00264 if (cd_bb != NULL)
00265 return;
00266 cd_bb = temp_bb;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275 if (cd_bb == NULL ||
00276 cd_bb->Kind() == BB_ENTRY ||
00277 cd_bb == bb ||
00278 !cd_bb->Dominates(bb))
00279 return;
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 if (!etable->Str_red()->Update_happens_rarely_enough(cd_bb, bb, NULL))
00300 return;
00301
00302 BB_LIST_ITER bb_iter;
00303 BB_NODE *succ;
00304 INT i = 0;
00305 _which_cd_succ = (UINT) -1;
00306 FOR_ALL_ELEM( succ, bb_iter, Init(cd_bb->Succ()) ) {
00307 if (bb->Postdominates(succ)) {
00308 _which_cd_succ = i;
00309 break;
00310 }
00311 i++;
00312 }
00313 Is_True(_which_cd_succ != (UINT) -1,
00314 ("HOIST_SUMMARY::Init_cfg_info: cannot find succ that bb postdom."));
00315
00316
00317
00318
00319 if (cd_bb->Succ()->Len() >= sizeof(UINT)*8) {
00320 _which_cd_succ = 0;
00321 return;
00322 }
00323
00324 _cd_hs = &summary[cd_bb->Id()];
00325 }
00326
00327
00328
00329
00330
00331
00332
00333 inline void
00334 EXP_HOISTING::Update_succ_count_rec(HOIST_SUMMARY *hs, UINT which_succ, CODEREP *expr, EXP_OCCURS *exp_occur)
00335 {
00336
00337 if (hs->Exp_id() != Cur_exp_id())
00338 hs->Init_per_expr(Cur_exp_id());
00339
00340 Is_True(0 <= (INT) which_succ && which_succ < hs->Bb()->Succ()->Len(),
00341 ("EXP_HOISTING::Update_succ_count_rec: invalid which_succ."));
00342
00343 if (hs->Exp_occur() == NULL && exp_occur != NULL)
00344 hs->Set_exp_occur(exp_occur);
00345
00346
00347
00348 if (!hs->Succ_processed(which_succ)) {
00349
00350
00351 hs->Update_succ_count(which_succ);
00352
00353 if (hs->Anticipated()) {
00354 Is_True(hs->Exp_occur() != NULL,
00355 ("EXP_HOISTING::Update_succ_count_rec: "
00356 "hs is fully anticipated but has null exp_occur."));
00357 }
00358
00359 Is_Trace(Etable()->Tracing(),
00360 (TFile, "EXP_HOISTING: update succ count of BB%d due to e-ver %d%s\n",
00361 hs->Bb()->Id(),
00362 exp_occur != NULL ? (INT) exp_occur->E_version() : -1,
00363 hs->Anticipated() ? ", becomes fully anticipated." : "."));
00364
00365 if (hs->Anticipated() &&
00366 hs->Cd_hs() != NULL &&
00367
00368
00369 (exp_occur != NULL || hs->Cd_hs()->Exp_occur() != NULL) &&
00370
00371 hs->Anticipated_at_CD(expr) &&
00372 hs->Def_occur_allows_hoisting(exp_occur))
00373 Update_succ_count_rec(hs->Cd_hs(), hs->Which_cd_succ(), expr, exp_occur);
00374 }
00375 }
00376
00377
00378
00379
00380
00381 static
00382 BOOL CR_match_kids(CODEREP *cr1, CODEREP *cr2)
00383 {
00384 if (cr1 == cr2) return TRUE;
00385 if (cr1->Kind() != cr2->Kind()) return FALSE;
00386
00387 switch (cr1->Kind()) {
00388 case CK_IVAR:
00389 if (cr1->Ilod_base() != cr2->Ilod_base())
00390 return FALSE;
00391 if (cr1->Ivar_mu_node()->OPND() != cr2->Ivar_mu_node()->OPND())
00392 return FALSE;
00393 if (cr1->Opr() != OPR_ILOADX) {
00394 if (cr1->Offset() != cr2->Offset())
00395 return FALSE;
00396 }
00397 else {
00398 if (cr1->Index() != cr2->Index())
00399 return FALSE;
00400 }
00401 if (Get_mtype_class(cr1->Dtyp()) != Get_mtype_class(cr2->Dtyp()))
00402 return FALSE;
00403 if (MTYPE_size_min(cr1->Dsctyp()) != MTYPE_size_min(cr2->Dsctyp()))
00404 return FALSE;
00405
00406 Is_True(OPCODE_operator(cr1->Op()) != OPR_MLOAD,
00407 ("CR_match_kids: OPR_MLOAD not supported in EPRE."));
00408
00409 return TRUE;
00410
00411 case CK_OP:
00412 {
00413 if (cr1->Op() != cr2->Op()) return FALSE;
00414 for (INT i = 0; i < cr1->Kid_count(); i++)
00415 if (cr1->Opnd(i) != cr2->Opnd(i))
00416 return FALSE;
00417 if (OPCODE_operator(cr1->Op()) == OPR_INTRINSIC_OP &&
00418 cr1->Intrinsic() != cr2->Intrinsic())
00419 return FALSE;
00420 #ifdef KEY
00421 if (OPCODE_operator(cr1->Op()) == OPR_PURE_CALL_OP &&
00422 cr1->Call_op_aux_id() != cr2->Call_op_aux_id())
00423 return FALSE;
00424 #endif
00425 if (OPCODE_operator(cr1->Op()) == OPR_CVTL &&
00426 cr1->Offset() != cr2->Offset())
00427 return FALSE;
00428 return TRUE;
00429 }
00430 default:
00431 return FALSE;
00432 }
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 EXP_OCCURS *
00451 EXP_HOISTING::Get_hoisted_exp_occur(HOIST_SUMMARY *hs, CODEREP *cr, EXP_OCCURS *def_occur, BOOL real_occ_is_injured)
00452 {
00453 Is_True(hs->Anticipated(),
00454 ("EXP_HOISTING::Alloc_and_insert_exp_occur: expr not fully anticipated."));
00455 Is_True(hs->Exp_id() == Cur_exp_id(),
00456 ("EXP_HOISTING::wrong exp id %d in worklist of %d.", hs->Exp_id(), Cur_exp_id()));
00457
00458
00459 if (hs->Exp_id() == Cur_exp_id() &&
00460 hs->Hoisted_occur() != NULL) {
00461
00462
00463
00464
00465
00466
00467
00468
00469 Is_True(hs->Hoisted_occur()->Occurrence() == cr ||
00470 (cr->Kind() == CK_IVAR &&
00471 (def_occur == NULL ||
00472 CR_match_kids(hs->Hoisted_occur()->Occurrence(),def_occur->Occurrence()))) ||
00473 cr->Coderep_id() == 0 ||
00474 real_occ_is_injured,
00475 ("EXP_HOISTING::Get_hoisted_exp_occur: CRs (cr%d) and (cr%d) are different!",
00476 hs->Hoisted_occur()->Occurrence()->Coderep_id(), cr->Coderep_id()));
00477
00478 if (real_occ_is_injured)
00479 hs->Set_injured();
00480
00481 return hs->Hoisted_occur();
00482 }
00483
00484 Is_Trace(Etable()->Tracing(),
00485 (TFile, "EXP_HOISTING: hs->Cd_hs():\n"));
00486 if (hs->Cd_hs() != NULL && hs->Cd_hs()->Exp_id() == Cur_exp_id()) {
00487 Is_Trace_cmd(Etable()->Tracing(), hs->Cd_hs()->Print(TFile));
00488 }
00489 else if (hs->Cd_hs() == NULL) {
00490 Is_Trace(Etable()->Tracing(), (TFile, " is NULL\n"));
00491 } else {
00492 Is_Trace(Etable()->Tracing(), (TFile, " has diff exp-id\n"));
00493 }
00494
00495
00496
00497
00498
00499
00500 EXP_OCCURS *occ;
00501 if (hs->Cd_hs() != NULL &&
00502 hs->Cd_hs()->Exp_id() == Cur_exp_id() &&
00503 (hs->Cd_hs()->Exp_occur() == NULL ||
00504 CR_match_kids(hs->Cd_hs()->Exp_occur()->Occurrence(),cr)) &&
00505 hs->Def_occur_allows_hoisting(def_occur) &&
00506 hs->Cd_hs()->Anticipated()) {
00507
00508
00509 occ = Get_hoisted_exp_occur(hs->Cd_hs(), cr, def_occur, real_occ_is_injured);
00510
00511 if (hs->Cd_hs()->Injured())
00512 hs->Set_injured();
00513 } else {
00514
00515
00516
00517
00518
00519 if (def_occur != NULL &&
00520 (def_occur->Bb() == hs->Bb() ||
00521 def_occur->Bb()->Dominates(hs->Bb())) &&
00522 (def_occur->Occ_kind() == EXP_OCCURS::OCC_REAL_OCCUR ||
00523 (def_occur->Occ_kind() == EXP_OCCURS::OCC_PHI_OCCUR &&
00524 def_occur->Exp_phi()->Will_b_avail()))) {
00525
00526 occ = def_occur;
00527 if (def_occur->Occurrence()->Coderep_id() == 0)
00528 def_occur->Set_occurrence(cr);
00529
00530 Is_True(def_occur->Occurrence() == cr || real_occ_is_injured ||
00531 (cr->Kind() == CK_IVAR &&
00532 (def_occur->Occurrence()->Ilod_base() == cr->Ilod_base() ||
00533 def_occur->Occurrence()->Istr_base() == cr->Ilod_base())),
00534 ("EXP_HOISTING::Get_hoisted_exp_occur: def_occur and cr not match."));
00535
00536 } else {
00537
00538
00539 BOOL tree_changed = FALSE;
00540 if (cr->Coderep_id() == 0) {
00541 if (cr->Kind() == CK_IVAR) {
00542 cr->Set_ivar_mu_node(CXX_NEW(MU_NODE(*cr->Ivar_mu_node()),
00543 Etable()->Opt_stab()->Occ_pool()));
00544 cr->Set_ivar_occ(CXX_NEW(OCC_TAB_ENTRY(*cr->Ivar_occ()),
00545 Etable()->Opt_stab()->Occ_pool()));
00546 }
00547
00548
00549
00550 if (cr->Is_integral_load_store()) {
00551 cr = Exp_worklst()->Save_use_cr(Etable(), cr);
00552 }
00553
00554
00555 cr = Etable()->Htable()->Rehash_tree(cr, FALSE,
00556 &tree_changed, hs->Bb());
00557 cr->Set_e_num(Cur_exp_id());
00558 }
00559
00560
00561 EXP_OCCURS *new_occ = Etable()->Alloc_occurs_node(cr);
00562
00563 new_occ->Set_hoisted_occ();
00564 new_occ->Set_enclose_bb(hs->Bb());
00565 if (def_occur != NULL &&
00566 (def_occur->Bb() == hs->Bb() ||
00567 def_occur->Bb()->Dominates(hs->Bb())) &&
00568 def_occur->Occ_kind() == EXP_OCCURS::OCC_PHI_OCCUR) {
00569 new_occ->Set_e_version(def_occur->E_version());
00570 new_occ->Set_def_occur(def_occur);
00571 occ = new_occ;
00572 } else {
00573 new_occ->Set_e_version(Exp_worklst()->Cur_e_version());
00574 Exp_worklst()->New_e_version();
00575 new_occ->Set_def_occur(NULL);
00576 occ = new_occ;
00577 }
00578
00579 if (tree_changed) {
00580 new_occ->Set_rehash_changed_expr();
00581 }
00582
00583
00584
00585
00586
00587
00588
00589 Warn_todo("EXP_HOISTING::Generate_hoisted_exp_occur: "
00590 "inefficient insertion.");
00591
00592 Exp_worklst()->Insert_occurrence(new_occ, hs->Bb());
00593 Is_Trace(Etable()->Tracing(),
00594 (TFile, "HOISTING: Insert hoisted expr version %d (cr%d) at BB%d\n",
00595 occ->E_version(), cr->Coderep_id(), hs->Bb()->Id()));
00596 }
00597 }
00598 if (real_occ_is_injured)
00599 hs->Set_injured();
00600 hs->Set_hoisted_occur(occ);
00601 return occ;
00602 }
00603
00604
00605
00606
00607 inline BOOL
00608 Lod_modified(const BB_NODE *bb, const CODEREP *cr)
00609 {
00610 Is_True(cr->Kind() == CK_VAR, ("Lod_modified: expecting CK_VAR."));
00611 Is_True(!cr->Is_flag_set(CF_IS_ZERO_VERSION), ("Lod_modified: zero ver not allowed."));
00612 if (cr->Defbb() != bb) {
00613
00614 if (cr->Defbb()->Dominates(bb))
00615 return FALSE;
00616 } else
00617
00618
00619 return FALSE;
00620 return TRUE;
00621 }
00622
00623
00624
00625
00626 BOOL
00627 HOIST_SUMMARY::Anticipated_at_CD(CODEREP *cr) const
00628 {
00629 BB_NODE *cd_bb = Cd_hs()->Bb();
00630
00631 switch (cr->Kind()) {
00632 case CK_RCONST:
00633 case CK_CONST:
00634 case CK_LDA:
00635 return TRUE;
00636 case CK_VAR:
00637 return !Lod_modified(cd_bb, cr);
00638 case CK_IVAR: {
00639 CODEREP *addr = cr->Ilod_base() ? cr->Ilod_base() : cr->Istr_base();
00640 if (addr->Kind() == CK_VAR) {
00641 if (Lod_modified(cd_bb, addr))
00642 return FALSE;
00643 }
00644
00645 const CODEREP *cr_vsym = cr->Ivar_mu_node()->OPND();
00646 if (Lod_modified(cd_bb, cr_vsym))
00647 return FALSE;
00648 return TRUE;
00649 }
00650
00651 case CK_OP: {
00652 for (INT i = 0; i < cr->Kid_count(); i++) {
00653 CODEREP *kid = cr->Opnd(i);
00654 switch (kid->Kind()) {
00655 case CK_VAR:
00656 if (Lod_modified(cd_bb, kid))
00657 return FALSE;
00658 break;
00659 case CK_IVAR:
00660 return FALSE;
00661 case CK_CONST:
00662 case CK_RCONST:
00663 case CK_LDA:
00664 break;
00665 default:
00666 Is_True(FALSE, ("unexpected CK_KIND."));
00667 }
00668 }
00669 return TRUE;
00670 }
00671 default:
00672 Is_True(FALSE, ("unexpected CK_KIND."));
00673 }
00674
00675 return TRUE;
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 inline BOOL
00687 HOIST_SUMMARY::Def_occur_allows_hoisting(EXP_OCCURS *def_occur) const
00688 {
00689 if (def_occur == NULL ||
00690 def_occur->Occ_kind() != EXP_OCCURS::OCC_PHI_OCCUR ||
00691 def_occur->Bb()->Dominates( Cd_hs()->Bb() ))
00692 return TRUE;
00693 return FALSE;
00694 }
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711 void
00712 EXP_HOISTING::Generate_hoisted_occur(EXP_WORKLST *exp_worklst)
00713 {
00714 EXP_OCCURS *real_occ;
00715 EXP_OCCURS *phi_pred;
00716 EXP_OCCURS_ITER real_occ_iter;
00717 EXP_OCCURS_ITER phi_pred_iter;
00718
00719 _exp_worklst = exp_worklst;
00720 Set_cur_exp_id(Exp_worklst()->E_num());
00721 Clear_hoist_worklist();
00722
00723 FOR_ALL_NODE (real_occ, real_occ_iter, Init(Exp_worklst()->Real_occurs().Head())) {
00724 HOIST_SUMMARY *hs = Summary(real_occ->Bb()->Id());
00725 if (hs->Exp_id() != Cur_exp_id())
00726 hs->Init_per_expr(Cur_exp_id());
00727
00728 if (!real_occ->Occurs_as_lvalue() &&
00729 hs->Cd_hs() != NULL &&
00730 hs->Exp_occur() == NULL &&
00731 hs->Anticipated_at_CD(real_occ->Occurrence()) &&
00732 hs->Def_occur_allows_hoisting(real_occ->Def_occur())) {
00733 hs->Set_exp_occur(real_occ);
00734 Append_hoist_worklist(hs);
00735 Update_succ_count_rec(hs->Cd_hs(), hs->Which_cd_succ(), real_occ->Occurrence(), real_occ);
00736 }
00737 }
00738
00739 FOR_ALL_NODE (phi_pred, phi_pred_iter, Init(Exp_worklst()->Phi_pred_occurs().Head())) {
00740
00741 HOIST_SUMMARY *hs = Summary(phi_pred->Bb()->Id());
00742 if (hs->Exp_id() != Cur_exp_id())
00743 hs->Init_per_expr(Cur_exp_id());
00744
00745 BB_LIST_ITER succ_bb_iter;
00746 BB_NODE *succ_bb;
00747 CODEREP *insert_cr;
00748 INT opnd_num = -1;
00749 EXP_PHI *exp_phi = NULL;
00750 FOR_ALL_ELEM(succ_bb, succ_bb_iter, Init(phi_pred->Bb()->Succ())) {
00751 EXP_PHI *exp_phi = Etable()->Lookup_exp_phi(succ_bb, Exp_worklst()->Exp());
00752 if (exp_phi != NULL && exp_phi->Will_b_avail()) {
00753 opnd_num = succ_bb->Pred()->Pos(phi_pred->Bb());
00754 if (exp_phi->Need_insertion(opnd_num)) {
00755 insert_cr = Etable()->
00756 Alloc_and_generate_cur_expr(exp_phi->Result()->Occurrence(),
00757 succ_bb,
00758 opnd_num,
00759 Etable()->Per_expr_pool(),
00760 TRUE );
00761 if (hs->Cd_hs() != NULL &&
00762 hs->Exp_occur() == NULL &&
00763
00764
00765
00766
00767
00768
00769 hs->Cd_hs()->Exp_occur() != NULL &&
00770
00771 hs->Anticipated_at_CD(insert_cr) &&
00772 hs->Def_occur_allows_hoisting(exp_phi->Opnd(opnd_num))) {
00773 Append_hoist_worklist(hs);
00774 hs->Set_exp_occur(phi_pred);
00775 Update_succ_count_rec(hs->Cd_hs(), hs->Which_cd_succ(), insert_cr, NULL);
00776 break;
00777 }
00778 }
00779 }
00780 }
00781 }
00782
00783
00784
00785
00786 HOIST_SUMMARY *hs;
00787 for (hs = _head; hs != NULL; hs = hs->Next()) {
00788
00789 Is_True(hs->Exp_id() == Cur_exp_id(),
00790 ("EXP_HOISTING::Generate_hoisted_occur: uninitialized var."));
00791
00792 Is_Trace(Etable()->Tracing(),
00793 (TFile, "-------\nEXP_HOISTING: Considering hs:\n"));
00794 Is_Trace_cmd(Etable()->Tracing(), hs->Print(TFile));
00795
00796
00797 if (hs->Cd_hs() != NULL &&
00798 hs->Cd_hs()->Exp_id() == Cur_exp_id() &&
00799 hs->Cd_hs()->Anticipated()) {
00800
00801 Is_Trace(Etable()->Tracing(), (TFile, "EXP_HOISTING: May hoist...\n"));
00802
00803 if (hs->Exp_occur()->Occ_kind() == EXP_OCCURS::OCC_REAL_OCCUR) {
00804 EXP_OCCURS *real_occ = hs->Exp_occur();
00805
00806 if (hs->Cd_hs()->Hoisted_occur() != NULL) {
00807 if (!CR_match_kids(hs->Cd_hs()->Hoisted_occur()->Occurrence(), real_occ->Occurrence()))
00808 continue;
00809 }
00810
00811 EXP_OCCURS *hoisted_occ =
00812 Get_hoisted_exp_occur(hs->Cd_hs(), real_occ->Occurrence(),
00813 real_occ->Def_occur(), real_occ->Injured_occ());
00814 Is_Trace(Etable()->Tracing(),
00815 (TFile, " Get hoisted exp occur for real occ ver %d\n",
00816 real_occ->E_version()));
00817
00818 EXP_OCCURS *hoisted_def_occ =
00819 (hoisted_occ->Occ_kind() == EXP_OCCURS::OCC_REAL_OCCUR &&
00820 hoisted_occ->Def_occur() != NULL)
00821 ? hoisted_occ->Def_occur() : hoisted_occ;
00822
00823
00824
00825 if (hoisted_def_occ->E_version() != real_occ->E_version()) {
00826 IDTYPE e_ver = real_occ->E_version();
00827 BB_NODE *bb = real_occ->Bb();
00828 EXP_OCCURS *occ = real_occ;
00829 do {
00830 Is_Trace(Etable()->Tracing(),
00831 (TFile, "HOISTING: updating real occ in BB%d from ver %d to %d\n",
00832 occ->Bb()->Id(), occ->E_version(), hoisted_def_occ->E_version()));
00833 occ->Set_e_version(hoisted_def_occ->E_version());
00834 occ->Set_def_occur(hoisted_def_occ);
00835 Exp_worklst()->Inc_hoisted_count();
00836 occ = occ->Next();
00837 } while (occ != NULL && occ->Bb() == bb && occ->E_version() == e_ver);
00838 }
00839 }
00840 }
00841 }
00842
00843
00844
00845 for (hs = _head; hs != NULL; hs = hs->Next()) {
00846
00847 Is_True(hs->Exp_id() == Cur_exp_id(),
00848 ("EXP_HOISTING::Generate_hoisted_occur: uninitialized var."));
00849
00850 Is_Trace(Etable()->Tracing(),
00851 (TFile, "-------\nEXP_HOISTING: Considering hs:\n"));
00852 Is_Trace_cmd(Etable()->Tracing(), hs->Print(TFile));
00853
00854
00855 if (hs->Cd_hs() != NULL &&
00856 hs->Cd_hs()->Exp_id() == Cur_exp_id() &&
00857 hs->Cd_hs()->Anticipated()) {
00858
00859 Is_Trace(Etable()->Tracing(), (TFile, "EXP_HOISTING: May hoist...\n"));
00860
00861 if (hs->Exp_occur()->Occ_kind() == EXP_OCCURS::OCC_PHI_PRED_OCCUR) {
00862 EXP_OCCURS *insert_occ = hs->Exp_occur();
00863 BB_LIST_ITER succ_bb_iter;
00864 BB_NODE *succ_bb;
00865 CODEREP *insert_cr = NULL;
00866 INT opnd_num = -1;
00867 EXP_PHI *exp_phi = NULL;
00868 FOR_ALL_ELEM(succ_bb, succ_bb_iter, Init(insert_occ->Bb()->Succ())) {
00869 EXP_PHI *temp_phi = Etable()->Lookup_exp_phi(succ_bb, Exp_worklst()->Exp());
00870 if (temp_phi != NULL) {
00871 exp_phi = temp_phi;
00872 opnd_num = succ_bb->Pred()->Pos(insert_occ->Bb());
00873 if (exp_phi->Need_insertion(opnd_num)) {
00874 insert_cr = Etable()->Get_cached_cur_expr(succ_bb, opnd_num);
00875 Is_True(insert_cr, ("EXP_HOISTING::Generate_hoisted_occur: unable to get cached expr."));
00876 Is_True(inCODEKIND(insert_cr->Kind(),CK_VAR|CK_RCONST|CK_CONST|CK_LDA)
00877 || insert_cr->Coderep_id() == 0,
00878 ("EXP_HOISTING::Generate_hoisted_occ: valno not cleared."));
00879 break;
00880 }
00881 }
00882 }
00883
00884 Is_True(insert_cr != NULL,
00885 ("EXP_HOISTING::Generate_hoisted_occur: can't generate coderep."));
00886
00887 if (hs->Cd_hs()->Hoisted_occur() != NULL) {
00888 if (!CR_match_kids(hs->Cd_hs()->Hoisted_occur()->Occurrence(), insert_cr))
00889 continue;
00890 }
00891
00892 EXP_OCCURS *hoisted_occ = Get_hoisted_exp_occur(hs->Cd_hs(), insert_cr, NULL, FALSE);
00893
00894
00895
00896 BOOL contains_injured_real_occ = hs->Cd_hs()->Injured();
00897
00898 EXP_OCCURS *hoisted_def_occ =
00899 (hoisted_occ->Occ_kind() == EXP_OCCURS::OCC_REAL_OCCUR &&
00900 hoisted_occ->Def_occur() != NULL)
00901 ? hoisted_occ->Def_occur() : hoisted_occ;
00902
00903 Is_Trace(Etable()->Tracing(),
00904 (TFile, "HOISTING: updating insert occ in BB%d to ver %d\n",
00905 insert_occ->Bb()->Id(), hoisted_occ->E_version()));
00906 exp_phi->Set_opnd(opnd_num, hoisted_def_occ);
00907
00908
00909
00910 exp_phi->Set_has_real_occ(opnd_num);
00911
00912
00913 insert_occ->Reset_inserted();
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927 if (contains_injured_real_occ) {
00928 exp_phi->Set_injured(opnd_num);
00929 Etable()->Update_cached_cur_expr(succ_bb, opnd_num,
00930 hoisted_occ->Occurrence());
00931 exp_phi->Pred(opnd_num)->Set_occurrence(hoisted_occ->Occurrence());
00932 }
00933
00934 Exp_worklst()->Inc_hoisted_count();
00935 }
00936 }
00937 }
00938
00939 Etable()->Inc_hoisted(Exp_worklst()->Hoisted_count());
00940
00941
00942
00943
00944
00945
00946
00947
00948 FOR_ALL_NODE (real_occ, real_occ_iter, Init(Exp_worklst()->Real_occurs().Head())) {
00949 if (!real_occ->Occurs_as_lvalue() &&
00950 real_occ->Def_occur() != NULL &&
00951 real_occ->Def_occur()->E_version() != real_occ->E_version()) {
00952 Is_Trace(Etable()->Tracing(),
00953 (TFile, "HOISTING: This is not a bug. "
00954 "e-num %d : e-version %d is renamed to e-version %d.",
00955 Exp_worklst()->E_num(),
00956 real_occ->E_version(),
00957 real_occ->Def_occur()->E_version()));
00958 real_occ->Set_e_version(real_occ->Def_occur()->E_version());
00959 }
00960 }
00961 }
00962
00963
00964
00965
00966
00967
00968 EXP_HOISTING *
00969 New_EXP_HOISTING(ETABLE *etable, MEM_POOL *pool)
00970 {
00971 return CXX_NEW(EXP_HOISTING(etable, pool), pool);
00972 }
00973
00974
00975 void
00976 Delete_EXP_HOISTING(EXP_HOISTING *eh)
00977 {
00978 CXX_DELETE(eh, eh->Pool());
00979 }
00980
00981
00982 void
00983 EXP_WORKLST::Hoist_expression(EXP_HOISTING *exp_hoist)
00984 {
00985 if (!WOPT_Enable_Ivar_Hoisting && Exp()->Kind() == CK_IVAR)
00986 return;
00987
00988 exp_hoist->Generate_hoisted_occur(this);
00989 Is_True(Verify_dpo_order(Real_occurs()),
00990 ("EXP_WORKLST::Hoist_expression: Not in DPO order after "
00991 "hoisted real_occurrence insertion"));
00992 }