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
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 #ifdef USE_PCH
00097 #include "opt_pch.h"
00098 #endif // USE_PCH
00099 #pragma hdrstop
00100
00101
00102 #define __STDC_LIMIT_MACROS
00103 #include <stdint.h>
00104 #include "defs.h"
00105 #include "errors.h"
00106 #include "erglob.h"
00107 #include "glob.h"
00108 #include "mempool.h"
00109 #include "tracing.h"
00110 #include "cxx_memory.h"
00111 #include "config_targ.h"
00112
00113 #include "opt_sys.h"
00114 #include "opt_defs.h"
00115 #include "opt_cfg.h"
00116 #include "opt_ssa.h"
00117 #include "opt_main.h"
00118 #include "opt_mu_chi.h"
00119 #include "opt_sym.h"
00120 #include "opt_htable.h"
00121 #include "bb_node_set.h"
00122 #include "opt_bdce.h"
00123
00124
00125
00126
00127
00128
00129 void
00130 BITWISE_DCE::Initialize_stmts_dead(void)
00131 {
00132 CFG_ITER cfg_iter(Cfg());
00133 BB_NODE *bb;
00134
00135 FOR_ALL_NODE( bb, cfg_iter, Init() ) {
00136
00137 STMTREP_ITER stmt_iter(bb->Stmtlist());
00138 STMTREP *stmt;
00139 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
00140 switch (stmt->Opr()) {
00141 case OPR_LABEL:
00142 case OPR_PRAGMA:
00143 case OPR_ALTENTRY:
00144 case OPR_GOTO:
00145 continue;
00146 default:
00147 stmt->Reset_live_stmt();
00148 }
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156
00157 BOOL
00158 BITWISE_DCE::Operators_without_dependency(OPERATOR opr)
00159 {
00160 if (OPERATOR_is_call(opr))
00161 return TRUE;
00162 switch (opr) {
00163 case OPR_ASM_STMT:
00164 case OPR_ASSERT:
00165 case OPR_PREFETCH:
00166 case OPR_XPRAGMA:
00167 case OPR_REGION:
00168 case OPR_FORWARD_BARRIER:
00169 case OPR_BACKWARD_BARRIER:
00170 case OPR_DEALLOCA:
00171 case OPR_EVAL:
00172 return TRUE;
00173 default:
00174 return FALSE;
00175 }
00176 }
00177
00178
00179
00180
00181 UINT64
00182 BITWISE_DCE::Bitmask_of_size(UINT64 vsize)
00183 {
00184 Is_True(vsize != 0, ("BITWISE_DCE::Bitmask_of_size: size cannot be 0"));
00185 if (vsize >= 64)
00186 return UINT64_MAX;
00187 return ((UINT64) 1 << vsize) - 1;
00188 }
00189
00190
00191
00192
00193
00194 UINT64
00195 BITWISE_DCE::Fill_lower_bits(UINT64 bitmask)
00196 {
00197 bitmask |= bitmask >> 32;
00198 bitmask |= bitmask >> 16;
00199 bitmask |= bitmask >> 8;
00200 bitmask |= bitmask >> 4;
00201 bitmask |= bitmask >> 2;
00202 bitmask |= bitmask >> 1;
00203 return bitmask;
00204 }
00205
00206
00207
00208
00209
00210
00211 inline UINT64
00212 BITWISE_DCE::Bits_in_type(MTYPE dt)
00213 {
00214 #ifndef KEY
00215 Is_True(dt != MTYPE_UNKNOWN, ("BITWISE_DCE::Bits_in_type: type is unknown"));
00216 #endif
00217 if (dt == MTYPE_V || dt == MTYPE_M || dt == MTYPE_UNKNOWN)
00218 return UINT64_MAX;
00219 UINT64 vsize = MTYPE_size_min(dt);
00220 return Bitmask_of_size(vsize);
00221 }
00222
00223
00224
00225
00226
00227
00228 inline UINT64
00229 BITWISE_DCE::Bits_in_coderep_result(CODEREP *cr)
00230 {
00231 if (cr->Kind() == CK_OP && cr->Opr() == OPR_CVT) {
00232 MTYPE dsctyp = cr->Dsctyp();
00233 MTYPE dtyp = cr->Dtyp();
00234 if (MTYPE_is_integral(dtyp) && MTYPE_is_integral(dsctyp))
00235 return Bitmask_of_size(MAX(MTYPE_size_min(dtyp), MTYPE_size_min(dsctyp)));
00236 return Bitmask_of_size(MTYPE_size_min(dtyp));
00237 }
00238 return Bits_in_type(cr->Dtyp());
00239 }
00240
00241
00242
00243
00244
00245
00246 UINT64
00247 BITWISE_DCE::Bits_in_var(CODEREP *v)
00248 {
00249 AUX_STAB_ENTRY *aux = Opt_stab()->Aux_stab_entry(v->Aux_id());
00250 if (aux->Is_dedicated_preg() || ! aux->Is_real_var())
00251 return UINT64_MAX;
00252
00253
00254 if (ST_class(Opt_stab()->Aux_stab_entry(v->Aux_id())->St()) == CLASS_PREG)
00255 return Bits_in_type(MTYPE_I8);
00256 if (aux->Byte_size() != 0)
00257 return Bitmask_of_size(aux->Byte_size() * 8);
00258 return Bits_in_type(v->Dsctyp());
00259 }
00260
00261
00262
00263
00264 void
00265 BITWISE_DCE::Mark_entire_var_live(CODEREP *cr, BOOL stmt_visit)
00266 {
00267 if (Tracing())
00268 fprintf(TFile, "Mark_entire_var_live(cr%d,%d)\n",
00269 cr->Coderep_id(), stmt_visit);
00270
00271 if (stmt_visit)
00272 IncUsecnt(cr);
00273
00274 if (! More_bits_live(cr, Bits_in_var(cr)))
00275 return;
00276
00277 stmt_visit = _copy_propagate && (Livebits(cr) == 0);
00278
00279 Union_livebits(cr, Bits_in_var(cr));
00280 if (cr->Is_flag_set(CF_DEF_BY_PHI)) {
00281 PHI_NODE *phi = cr->Defphi();
00282 PHI_OPND_ITER phi_opnd_iter(phi);
00283 CODEREP *opnd;
00284 FOR_ALL_ELEM(opnd, phi_opnd_iter, Init()) {
00285 if (! opnd->Is_flag_set(CF_IS_ZERO_VERSION))
00286 Mark_entire_var_live(opnd, stmt_visit);
00287 }
00288 }
00289 else if (cr->Defstmt())
00290 Mark_stmt_live(cr->Defstmt());
00291 }
00292
00293
00294
00295
00296
00297
00298 void
00299 BITWISE_DCE::Mark_var_bits_live(CODEREP *cr, UINT64 live_bits,
00300 BOOL stmt_visit)
00301 {
00302 if (Tracing())
00303 fprintf(TFile, "Mark_var_bits_live(cr%d,%d)\n",
00304 cr->Coderep_id(), stmt_visit);
00305
00306 live_bits &= Bits_in_var(cr);
00307
00308 if (stmt_visit)
00309 IncUsecnt(cr);
00310
00311 if (! More_bits_live(cr, live_bits))
00312 return;
00313
00314 stmt_visit = _copy_propagate && (Livebits(cr) == 0);
00315
00316 Union_livebits(cr, live_bits);
00317 if (cr->Is_flag_set(CF_DEF_BY_PHI)) {
00318 PHI_NODE *phi = cr->Defphi();
00319 PHI_OPND_ITER phi_opnd_iter(phi);
00320 CODEREP *opnd;
00321 FOR_ALL_ELEM(opnd, phi_opnd_iter, Init()) {
00322 if (! opnd->Is_flag_set(CF_IS_ZERO_VERSION))
00323 Mark_var_bits_live(opnd, live_bits, stmt_visit);
00324 }
00325 }
00326 else if (cr->Is_flag_set(CF_DEF_BY_CHI)) {
00327 if (! cr->Is_flag_set(CF_IS_ZERO_VERSION))
00328 Mark_stmt_live(cr->Defstmt());
00329 }
00330 else {
00331 if (cr->Defstmt()) {
00332 #ifdef KEY // bug 2666
00333 Mark_tree_bits_live(cr->Defstmt()->Rhs(), live_bits, stmt_visit);
00334 #else
00335 Mark_tree_bits_live(cr->Defstmt()->Rhs(), live_bits, FALSE);
00336 #endif
00337 }
00338
00339 }
00340 }
00341
00342
00343
00344
00345
00346
00347
00348 void
00349 BITWISE_DCE::Mark_tree_bits_live(CODEREP *cr, UINT64 live_bits,
00350 BOOL stmt_visit)
00351 {
00352 UINT64 new_livebits;
00353 if (Tracing())
00354 fprintf(TFile, "Mark_tree_bits_live(cr%d,%llx,%d)\n",
00355 cr->Coderep_id(), live_bits, stmt_visit);
00356
00357 if (stmt_visit && cr->Kind() != CK_VAR)
00358 if (Usecnt(cr) < 2)
00359 IncUsecnt(cr);
00360 else
00361 stmt_visit = FALSE;
00362
00363 BOOL visit_all = (stmt_visit || Livebits(cr) == 0);
00364
00365 switch (cr->Kind()) {
00366 case CK_CONST:
00367 case CK_RCONST:
00368 case CK_LDA:
00369 Union_livebits(cr, live_bits);
00370 return;
00371
00372 case CK_VAR:
00373 new_livebits = live_bits & Bits_in_var(cr);
00374 if ((MTYPE_signed(cr->Dsctyp()) || MTYPE_size_min(cr->Dsctyp()) == 32) &&
00375 (live_bits >> MTYPE_size_min(cr->Dsctyp())) != 0) {
00376
00377 new_livebits |= (1 << (MTYPE_size_min(cr->Dsctyp()) - 1));
00378 }
00379
00380 if (Bits_in_var(cr) == new_livebits)
00381 Mark_entire_var_live(cr, stmt_visit);
00382 else
00383 Mark_var_bits_live(cr, new_livebits, stmt_visit);
00384 return;
00385
00386 case CK_IVAR:
00387 if (cr->Opr() != OPR_PARM) {
00388 if (visit_all) {
00389 Mark_tree_bits_live(cr->Ilod_base(), Bits_in_type(Pointer_type),
00390 stmt_visit);
00391 if (cr->Opr() == OPR_MLOAD)
00392 Mark_tree_bits_live(cr->Mload_size(),
00393 Bits_in_coderep_result(cr->Mload_size()),
00394 stmt_visit);
00395 else if (cr->Opr() == OPR_ILOADX)
00396 Mark_tree_bits_live(cr->Index(),
00397 Bits_in_coderep_result(cr->Index()),
00398 stmt_visit);
00399 MU_NODE *mnode = cr->Ivar_mu_node();
00400 if (mnode && ! mnode->OPND()->Is_flag_set(CF_IS_ZERO_VERSION))
00401 Mark_entire_var_live(mnode->OPND(), stmt_visit);
00402 }
00403
00404 new_livebits = live_bits & Bits_in_type(cr->Dsctyp());
00405 if ((MTYPE_signed(cr->Dsctyp()) || MTYPE_size_min(cr->Dsctyp()) == 32) &&
00406 (live_bits >> MTYPE_size_min(cr->Dsctyp())) != 0) {
00407
00408 new_livebits |= (1 << (MTYPE_size_min(cr->Dsctyp()) - 1));
00409 }
00410
00411 if (More_bits_live(cr, new_livebits))
00412 Union_livebits(cr, new_livebits);
00413 return;
00414 }
00415 else {
00416 live_bits &= Bits_in_type(cr->Dtyp());
00417
00418 if (! stmt_visit && ! More_bits_live(cr, live_bits))
00419 return;
00420
00421 Union_livebits(cr, live_bits);
00422 Mark_tree_bits_live(cr->Ilod_base(), live_bits, stmt_visit);
00423 MU_NODE *mnode = cr->Ivar_mu_node();
00424 if (mnode && ! mnode->OPND()->Is_flag_set(CF_IS_ZERO_VERSION))
00425 Mark_entire_var_live(mnode->OPND(), stmt_visit);
00426 return;
00427 }
00428
00429 case CK_OP: {
00430 INT32 i;
00431 OPERATOR opr = cr->Opr();
00432
00433
00434 switch (opr) {
00435 case OPR_EQ: case OPR_NE:
00436 case OPR_GE: case OPR_GT: case OPR_LE: case OPR_LT:
00437 case OPR_LNOT:
00438 case OPR_LAND: case OPR_LIOR:
00439 live_bits &= 1;
00440 break;
00441 default: ;
00442 }
00443
00444 if (! stmt_visit && ! More_bits_live(cr, live_bits))
00445 return;
00446
00447 Union_livebits(cr, live_bits);
00448 MTYPE dtyp = cr->Dtyp();
00449 MTYPE dsctyp = (cr->Dsctyp() == MTYPE_V) ? dtyp : cr->Dsctyp();
00450
00451 if (Only_Unsigned_64_Bit_Ops &&
00452 (opr == OPR_DIV || opr == OPR_DIVREM || opr == OPR_MOD || opr == OPR_REM))
00453 dsctyp = Mtype_TransferSize(MTYPE_A8, dsctyp);
00454
00455 switch (opr) {
00456
00457
00458
00459 case OPR_CVTL: {
00460 new_livebits = Livebits(cr) & Bitmask_of_size(cr->Offset());
00461 if (MTYPE_signed(dtyp) &&
00462 (Livebits(cr) >> cr->Offset()) != 0) {
00463 UINT64 sign_bit_mask = 1LL << (cr->Offset() - 1);
00464 new_livebits |= sign_bit_mask;
00465
00466 }
00467 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00468 return;
00469 }
00470
00471 case OPR_CVT:
00472 if (dsctyp == MTYPE_B)
00473 Mark_tree_bits_live(cr->Opnd(0), 1, stmt_visit);
00474 else if (MTYPE_is_integral(dtyp) && MTYPE_is_integral(dsctyp)) {
00475 new_livebits = Livebits(cr) & Bits_in_type(dtyp) & Bits_in_type(dsctyp);
00476 if ((dsctyp == MTYPE_I4 || MTYPE_size_min(dtyp) == 32) &&
00477 (Livebits(cr) >> 32) != 0)
00478 new_livebits |= (1 << 31);
00479 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00480 }
00481 else if (visit_all)
00482 Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00483 return;
00484
00485 case OPR_NEG:
00486 Mark_tree_bits_live(cr->Opnd(0),
00487 Bits_in_type(dsctyp) & Fill_lower_bits(Livebits(cr)),
00488 stmt_visit);
00489 return;
00490
00491 case OPR_EXTRACT_BITS:
00492 new_livebits = Livebits(cr) & Bitmask_of_size(cr->Op_bit_size());
00493 if (MTYPE_signed(dtyp) &&
00494 (Livebits(cr) >> cr->Op_bit_size()) != 0) {
00495 UINT64 sign_bit_mask = 1LL << (cr->Op_bit_size() - 1);
00496 new_livebits |= sign_bit_mask;
00497
00498 }
00499 #ifdef KEY
00500 if (Target_Byte_Sex == BIG_ENDIAN)
00501 Mark_tree_bits_live(cr->Opnd(0), new_livebits <<
00502 (MTYPE_bit_size(dtyp) - cr->Op_bit_offset() - cr->Op_bit_size()),
00503 stmt_visit);
00504 else
00505 #endif
00506 Mark_tree_bits_live(cr->Opnd(0), new_livebits << cr->Op_bit_offset(),
00507 stmt_visit);
00508 return;
00509
00510 case OPR_PAREN:
00511 case OPR_BNOT:
00512 case OPR_MINPART: case OPR_MAXPART:
00513 new_livebits = Bits_in_type(dsctyp) & Livebits(cr);
00514 if (MTYPE_size_min(dsctyp) == 32 && (Livebits(cr) >> 32) != 0)
00515 new_livebits |= (1 << 31);
00516 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00517 return;
00518
00519 case OPR_ABS:
00520 case OPR_LNOT:
00521 case OPR_RND: case OPR_TRUNC: case OPR_CEIL: case OPR_FLOOR:
00522 case OPR_SQRT: case OPR_RSQRT: case OPR_RECIP:
00523 case OPR_REALPART: case OPR_IMAGPART:
00524 case OPR_HIGHPART: case OPR_LOWPART:
00525 case OPR_TAS:
00526 #ifdef TARG_X8664
00527 case OPR_REPLICATE:
00528 case OPR_REDUCE_ADD:
00529 case OPR_REDUCE_MPY:
00530 case OPR_REDUCE_MAX:
00531 case OPR_REDUCE_MIN:
00532 case OPR_SHUFFLE:
00533 case OPR_ATOMIC_RSQRT:
00534 #endif
00535 if (visit_all)
00536 Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00537 return;
00538
00539
00540
00541 case OPR_ADD: case OPR_SUB: case OPR_MPY:
00542 Mark_tree_bits_live(cr->Opnd(0),
00543 Bits_in_type(dsctyp) & Fill_lower_bits(Livebits(cr)),
00544 stmt_visit);
00545 Mark_tree_bits_live(cr->Opnd(1),
00546 Bits_in_type(dsctyp) & Fill_lower_bits(Livebits(cr)),
00547 stmt_visit);
00548 return;
00549
00550 case OPR_BXOR:
00551 new_livebits = Livebits(cr) & Bits_in_type(dsctyp);
00552 if (MTYPE_size_min(dsctyp) == 32 && (Livebits(cr) >> 32) != 0)
00553 new_livebits |= (1 << 31);
00554 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00555 Mark_tree_bits_live(cr->Opnd(1), new_livebits, stmt_visit);
00556 return;
00557
00558 case OPR_XMPY: case OPR_HIGHMPY:
00559 case OPR_DIV: case OPR_MOD: case OPR_REM: case OPR_DIVREM:
00560 case OPR_MAX: case OPR_MIN: case OPR_MINMAX:
00561 case OPR_EQ: case OPR_NE:
00562 case OPR_GE: case OPR_GT: case OPR_LE: case OPR_LT:
00563 case OPR_LAND: case OPR_LIOR:
00564 case OPR_COMPLEX:
00565 if (visit_all) {
00566 Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00567 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dsctyp), stmt_visit);
00568 }
00569 return;
00570
00571 case OPR_BIOR: case OPR_BNOR:
00572 new_livebits = Livebits(cr) & Bits_in_type(dsctyp);
00573 if (MTYPE_size_min(dsctyp) == 32 && (Livebits(cr) >> 32) != 0)
00574 new_livebits |= (1 << 31);
00575 if (cr->Opnd(0)->Kind() == CK_CONST)
00576 Mark_tree_bits_live(cr->Opnd(1), new_livebits &
00577 (~cr->Opnd(0)->Const_val()), stmt_visit);
00578 else Mark_tree_bits_live(cr->Opnd(1), new_livebits, stmt_visit);
00579 if (cr->Opnd(1)->Kind() == CK_CONST)
00580 Mark_tree_bits_live(cr->Opnd(0), new_livebits &
00581 (~cr->Opnd(1)->Const_val()), stmt_visit);
00582 else Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00583 return;
00584
00585 case OPR_BAND:
00586 new_livebits = Livebits(cr) & Bits_in_type(dsctyp);
00587 if (MTYPE_size_min(dsctyp) == 32 && (Livebits(cr) >> 32) != 0)
00588 new_livebits |= (1 << 31);
00589 if (cr->Opnd(0)->Kind() == CK_CONST)
00590 Mark_tree_bits_live(cr->Opnd(1), new_livebits &
00591 cr->Opnd(0)->Const_val(), stmt_visit);
00592 else Mark_tree_bits_live(cr->Opnd(1), new_livebits, stmt_visit);
00593 if (cr->Opnd(1)->Kind() == CK_CONST)
00594 Mark_tree_bits_live(cr->Opnd(0), new_livebits &
00595 cr->Opnd(1)->Const_val(), stmt_visit);
00596 else Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00597 return;
00598
00599 case OPR_RROTATE:
00600 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dsctyp), stmt_visit);
00601 new_livebits = Livebits(cr) & Bits_in_type(dtyp);
00602 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00603 return;
00604
00605 case OPR_LSHR:
00606 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dsctyp), stmt_visit);
00607 if (cr->Opnd(1)->Kind() == CK_CONST) {
00608 INT64 shift_amt = cr->Opnd(1)->Const_val();
00609 #if defined(TARG_MIPS) || defined(TARG_X8664)
00610 if (MTYPE_size_min(dtyp) < MTYPE_size_min(MTYPE_U8))
00611 shift_amt = 31 & cr->Opnd(1)->Const_val();
00612 #elif TARG_IA64
00613 if ((shift_amt < 0) || (shift_amt >= MTYPE_size_min(dtyp))) shift_amt = MTYPE_size_min(dtyp) -1;
00614 #endif
00615 Mark_tree_bits_live(cr->Opnd(0),
00616 ((Bits_in_type(dsctyp) & live_bits) << shift_amt) &
00617 Bits_in_type(dsctyp), stmt_visit);
00618 }
00619 else Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00620 return;
00621
00622 case OPR_ASHR:
00623 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dsctyp), stmt_visit);
00624 if (cr->Opnd(1)->Kind() == CK_CONST) {
00625 INT64 shift_amt = cr->Opnd(1)->Const_val();
00626 #if defined(TARG_MIPS) || defined(TARG_X8664)
00627 if (MTYPE_size_min(dtyp) < MTYPE_size_min(MTYPE_U8))
00628 shift_amt = 31 & cr->Opnd(1)->Const_val();
00629 #elif TARG_IA64
00630 if ((shift_amt < 0) || (shift_amt >= MTYPE_size_min(dtyp))) shift_amt = MTYPE_size_min(dtyp) -1;
00631 #endif
00632 #ifdef KEY // need to do extra work to determine if the sign bit is live
00633 UINT64 sign_livebits;
00634 if (MTYPE_size_min(dtyp) < MTYPE_size_min(MTYPE_U8)) {
00635 if (shift_amt <= 31 && (live_bits >> (31 - shift_amt)))
00636 sign_livebits = 0x10000000;
00637 else sign_livebits = 0;
00638 }
00639 else {
00640 if (shift_amt <= 63 && (live_bits >> (63 - shift_amt)))
00641 sign_livebits = 0x1000000000000000ULL;
00642 else sign_livebits = 0;
00643 }
00644 #endif
00645 Mark_tree_bits_live(cr->Opnd(0),
00646 ((Bits_in_type(dsctyp) & live_bits) << shift_amt) &
00647 Bits_in_type(dsctyp)
00648 #ifdef KEY
00649 | sign_livebits
00650 #endif
00651 , stmt_visit);
00652 }
00653 else Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00654 return;
00655
00656 case OPR_SHL:
00657 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dsctyp), stmt_visit);
00658 if (cr->Opnd(1)->Kind() == CK_CONST) {
00659 INT64 shift_amt = cr->Opnd(1)->Const_val();
00660 UINT64 bit_mask = Bits_in_type(dsctyp);
00661 #if TARG_IA64
00662 if ((shift_amt < 0) || (shift_amt > MTYPE_size_min(dtyp))) bit_mask = 0;
00663 #endif
00664 Mark_tree_bits_live(cr->Opnd(0),
00665 ((bit_mask & live_bits) >> shift_amt) & bit_mask,
00666 stmt_visit);
00667 }
00668 else Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00669 return;
00670
00671 case OPR_COMPOSE_BITS:
00672 #ifdef KEY
00673 if (Target_Byte_Sex == BIG_ENDIAN)
00674 new_livebits = Livebits(cr) &
00675 ~(Bitmask_of_size(cr->Op_bit_size()) <<
00676 (MTYPE_bit_size(dtyp) - cr->Op_bit_offset() - cr->Op_bit_size()));
00677 else
00678 #endif
00679 new_livebits = Livebits(cr) &
00680 ~(Bitmask_of_size(cr->Op_bit_size()) << cr->Op_bit_offset());
00681 Mark_tree_bits_live(cr->Opnd(0), new_livebits, stmt_visit);
00682 #ifdef KEY
00683 if (Target_Byte_Sex == BIG_ENDIAN)
00684 new_livebits = (Livebits(cr) >> (MTYPE_bit_size(dtyp) - cr->Op_bit_offset() - cr->Op_bit_size())) &
00685 Bitmask_of_size(cr->Op_bit_size());
00686 else
00687 #endif
00688 new_livebits = (Livebits(cr) >> cr->Op_bit_offset()) &
00689 Bitmask_of_size(cr->Op_bit_size());
00690 Mark_tree_bits_live(cr->Opnd(1), new_livebits, stmt_visit);
00691 return;
00692
00693
00694
00695
00696
00697 case OPR_SELECT:
00698 Mark_tree_bits_live(cr->Opnd(0), Bits_in_type(dsctyp), stmt_visit);
00699 Mark_tree_bits_live(cr->Opnd(1), Bits_in_type(dtyp) & Livebits(cr),
00700 stmt_visit);
00701 Mark_tree_bits_live(cr->Opnd(2), Bits_in_type(dtyp) & Livebits(cr),
00702 stmt_visit);
00703 return;
00704
00705 case OPR_MADD: case OPR_MSUB:
00706 case OPR_NMADD: case OPR_NMSUB:
00707 if (visit_all)
00708 for (i = 0; i < cr->Kid_count(); i++)
00709 Mark_tree_bits_live(cr->Opnd(i), Bits_in_type(dsctyp), stmt_visit);
00710 return;
00711
00712 case OPR_CALL: case OPR_ICALL:
00713 case OPR_INTRINSIC_CALL: case OPR_INTRINSIC_OP:
00714 case OPR_FORWARD_BARRIER: case OPR_BACKWARD_BARRIER:
00715 case OPR_ALLOCA: case OPR_DEALLOCA:
00716 case OPR_ASM_STMT: case OPR_ASM_INPUT:
00717 #ifdef KEY
00718 case OPR_PURE_CALL_OP:
00719 #endif
00720 if (visit_all) {
00721 for (i = 0; i < cr->Kid_count(); i++) {
00722 Mark_tree_bits_live(cr->Opnd(i), UINT64_MAX, stmt_visit);
00723 }
00724 }
00725 return;
00726
00727 default:
00728 Is_True(FALSE,
00729 ("BITWISE_DCE::Mark_tree_bits_live: unexpected operator"));
00730 }
00731 Is_True(FALSE,
00732 ("BITWISE_DCE::Mark_tree_bits_live: missing return statement"));
00733 }
00734
00735 default:
00736 Is_True(FALSE,
00737 ("BITWISE_DCE::Mark_tree_bits_live: unexpected kind 0x%x",
00738 cr->Kind()));
00739 }
00740 Is_True(FALSE,
00741 ("BITWISE_DCE::Mark_tree_bits_live: missing return statement"));
00742 }
00743
00744
00745
00746
00747
00748
00749 void
00750 BITWISE_DCE::Mark_stmt_live(STMTREP *stmt)
00751 {
00752 if (stmt->Live_stmt())
00753 return;
00754 stmt->Set_live_stmt();
00755
00756 if (Tracing())
00757 fprintf(TFile, "Mark_stmt_live(Sid%d)\n", stmt->Stmt_id());
00758
00759 OPERATOR opr = stmt->Opr();
00760 if (opr == OPR_PREFETCH)
00761 Mark_tree_bits_live(stmt->Rhs()->Ilod_base(), Bits_in_type(Pointer_type),
00762 _copy_propagate );
00763 else if (opr == OPR_RETURN_VAL)
00764 Mark_tree_bits_live(stmt->Rhs(), Bits_in_type(stmt->Rtype()),
00765 _copy_propagate );
00766 else if (! OPERATOR_is_store(opr)) {
00767 if (stmt->Rhs() != NULL)
00768 Mark_tree_bits_live(stmt->Rhs(), Bits_in_coderep_result(stmt->Rhs()),
00769 _copy_propagate );
00770 }
00771 else if (opr == OPR_STID &&
00772 ST_class(Opt_stab()->Aux_stab_entry(stmt->Lhs()->Aux_id())->St())
00773 == CLASS_PREG) {
00774 Mark_tree_bits_live(stmt->Rhs(), Bits_in_coderep_result(stmt->Rhs()),
00775 _copy_propagate );
00776 }
00777 else {
00778 switch (opr) {
00779 case OPR_ISTOREX:
00780 case OPR_MSTORE:
00781 if (opr == OPR_MSTORE)
00782 Mark_tree_bits_live(stmt->Lhs()->Mstore_size(),
00783 Bits_in_coderep_result(stmt->Lhs()->Mstore_size()),
00784 _copy_propagate );
00785 else
00786 Mark_tree_bits_live(stmt->Lhs()->Index(),
00787 Bits_in_coderep_result(stmt->Lhs()->Index()),
00788 _copy_propagate );
00789
00790 case OPR_ISTORE:
00791 Mark_tree_bits_live(stmt->Lhs()->Istr_base(), Bits_in_type(Pointer_type),
00792 _copy_propagate );
00793
00794 case OPR_STID:
00795 if (opr != OPR_MSTORE) {
00796
00797 Mark_tree_bits_live(stmt->Rhs(), Bits_in_coderep_result(stmt->Rhs()) &
00798 Bits_in_type(stmt->Lhs()->Dsctyp()),
00799 _copy_propagate );
00800 }
00801 else Mark_tree_bits_live(stmt->Rhs(), UINT64_MAX,
00802 _copy_propagate );
00803 break;
00804 default:
00805 Is_True(FALSE, ("BITWISE_DCE::Mark_stmt_live: unexpected store stmt"));
00806 }
00807 }
00808
00809 if (stmt->Has_mu()) {
00810 MU_LIST *mu_list = stmt->Mu_list();
00811 if ( mu_list != NULL ) {
00812 MU_LIST_ITER mu_iter;
00813 MU_NODE *mnode;
00814 FOR_ALL_NODE( mnode, mu_iter, Init(mu_list) ) {
00815 if (mnode->OPND()->Is_flag_set(CF_IS_ZERO_VERSION))
00816 continue;
00817 Mark_entire_var_live(mnode->OPND(), _copy_propagate );
00818 }
00819 }
00820 }
00821
00822 if (stmt->Has_chi()) {
00823 CHI_LIST_ITER chi_iter;
00824 CHI_NODE *cnode;
00825 CHI_LIST *chi_list = stmt->Chi_list();
00826 FOR_ALL_NODE( cnode, chi_iter, Init(chi_list)) {
00827 if (! cnode->Live())
00828 continue;
00829 if (cnode->OPND()->Is_flag_set(CF_IS_ZERO_VERSION))
00830 continue;
00831 Mark_entire_var_live(cnode->OPND(), _copy_propagate );
00832 }
00833 }
00834
00835
00836 }
00837
00838
00839
00840
00841
00842
00843
00844 void
00845 BITWISE_DCE::Find_and_mark_cd_branch_live(BB_NODE *bb)
00846 {
00847 if (Cd_bbs()->MemberP(bb))
00848 return;
00849 Cd_bbs()->Union1D(bb);
00850
00851 STMTREP *stmt; STMTREP_ITER stmt_iter(bb->Stmtlist());
00852
00853 FOR_ALL_NODE_REVERSE(stmt, stmt_iter, Init()) {
00854 OPERATOR opr = stmt->Opr();
00855 if (opr == OPR_COMPGOTO ||
00856 opr == OPR_TRUEBR || opr == OPR_FALSEBR ||
00857 opr == OPR_REGION || opr == OPR_AGOTO) {
00858 Mark_stmt_live(stmt);
00859 return;
00860 }
00861 }
00862 Is_True(FALSE,
00863 ("BITWISE_DCE::Find_and_mark_cd_branch_live: cannot find branch"));
00864 return;
00865 }
00866
00867
00868
00869
00870
00871
00872
00873 void
00874 BITWISE_DCE::Make_bb_live(BB_NODE *bb)
00875 {
00876 if (Live_bbs()->MemberP(bb))
00877 return;
00878 Live_bbs()->Union1D(bb);
00879
00880 if (bb->Kind() == BB_ENTRY && bb != Cfg()->Fake_entry_bb()) {
00881 STMTREP *entry_chi = bb->Stmtlist()->Head();
00882 Is_True(OPCODE_operator(entry_chi->Op()) == OPR_OPT_CHI, ("cannot find entry chi."));
00883 Mark_stmt_live(entry_chi);
00884 }
00885
00886
00887 PHI_NODE *phi;
00888 PHI_LIST_ITER phi_iter;
00889 FOR_ALL_ELEM(phi, phi_iter, Init(bb->Phi_list())) {
00890 if (phi->Live() &&
00891 (phi->RESULT()->Is_flag_set(CF_IS_ZERO_VERSION) ||
00892 phi->RESULT()->Is_flag_set(CF_INCOMPLETE_USES))) {
00893 PHI_OPND_ITER phi_opnd_iter(phi);
00894 CODEREP *opnd;
00895 FOR_ALL_ELEM(opnd, phi_opnd_iter, Init()) {
00896 if (! opnd->Is_flag_set(CF_IS_ZERO_VERSION))
00897 Mark_entire_var_live(opnd, _copy_propagate );
00898 }
00899 }
00900 }
00901
00902
00903 STMTREP_ITER stmt_iter(bb->Stmtlist());
00904 STMTREP *stmt;
00905 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
00906 if (stmt->Live_stmt())
00907 continue;
00908 if ((stmt->Opr() == OPR_ISTORE || stmt->Opr() == OPR_MSTORE) &&
00909 stmt->Lhs()->Points_to(Opt_stab())->Restricted())
00910 Mark_stmt_live(stmt);
00911 else if (stmt->Volatile_stmt() ||
00912 stmt->Opr() == OPR_STID && stmt->Lhs()->Is_var_volatile() ||
00913 Operators_without_dependency(stmt->Opr()) ||
00914 stmt->Has_zero_version_chi())
00915 Mark_stmt_live(stmt);
00916 else if (stmt->Opr() == OPR_STID && Opt_stab()->Is_varargs_func()) {
00917 CODEREP *lhs = stmt->Lhs();
00918 ST *s = Opt_stab()->St(lhs->Aux_id());
00919 CODEREP *rhs = stmt->Rhs();
00920 if (ST_sclass(s) == SCLASS_FORMAL && rhs->Kind() == CK_VAR &&
00921 ST_class(Opt_stab()->St(rhs->Aux_id())) == CLASS_PREG &&
00922 Preg_Is_Dedicated(rhs->Offset()))
00923
00924
00925 Mark_stmt_live(stmt);
00926 }
00927 }
00928
00929 if (! bb->Willexit())
00930 return;
00931
00932
00933 BB_NODE *bby;
00934 BB_NODE_SET_ITER bns_iter;
00935 FOR_ALL_ELEM (bby, bns_iter, Init(bb->Rcfg_dom_frontier()))
00936 Find_and_mark_cd_branch_live(bby);
00937
00938
00939
00940 BB_LIST_ITER bb_iter;
00941 FOR_ALL_ELEM (bby, bb_iter, Init(bb->Pred())) {
00942 if (bby == bb) {
00943 Find_and_mark_cd_branch_live(bb);
00944 break;
00945 }
00946 }
00947 }
00948
00949
00950
00951
00952
00953
00954
00955
00956 void
00957 BITWISE_DCE::Find_and_mark_return_live(BB_NODE *bb)
00958 {
00959 BOOL return_found = FALSE;
00960 STMTREP *stmt;
00961 STMTREP_ITER stmt_iter(bb->Stmtlist());
00962
00963 FOR_ALL_NODE_REVERSE(stmt, stmt_iter, Init()) {
00964 if (stmt->Opr() == OPR_RETURN ||
00965 stmt->Opr() == OPR_RETURN_VAL ||
00966 #ifdef KEY
00967 stmt->Opr() == OPR_GOTO_OUTER_BLOCK ||
00968 #endif
00969 stmt->Opr() == OPR_REGION_EXIT) {
00970 return_found = TRUE;
00971 Mark_stmt_live(stmt);
00972 if (Tracing())
00973 fprintf(TFile, "Return stmt at BB %d\n", bb->Id());
00974 if (stmt->Opr() == OPR_RETURN_VAL)
00975 break;
00976 }
00977 else if (! return_found && ! OPERATOR_is_not_executable(stmt->Opr()))
00978 return;
00979 else if (stmt->Opr() == OPR_STID &&
00980 Opt_stab()->Aux_stab_entry(stmt->Lhs()->Aux_id())->Is_dedicated_preg())
00981 Mark_entire_var_live(stmt->Lhs(), FALSE);
00982 }
00983 return;
00984 }
00985
00986
00987
00988
00989
00990
00991 void
00992 BITWISE_DCE::Mark_willnotexit_stmts_live(BB_NODE *bb)
00993 {
00994 if (Tracing())
00995 fprintf(TFile, "Willnotexit BB %d\n", bb->Id());
00996
00997 Cd_bbs()->Union1D(bb);
00998
00999 STMTREP *stmt;
01000 STMTREP_ITER stmt_iter(bb->Stmtlist());
01001
01002 FOR_ALL_NODE_REVERSE(stmt, stmt_iter, Init())
01003 Mark_stmt_live(stmt);
01004
01005
01006 BB_NODE *pdom_bb;
01007 BB_LIST_ITER pdom_bb_iter;
01008 FOR_ALL_ELEM(pdom_bb, pdom_bb_iter, Init(bb->Pdom_bbs())) {
01009 if (! pdom_bb->Willexit())
01010 Mark_willnotexit_stmts_live(pdom_bb);
01011 }
01012 }
01013
01014
01015
01016
01017
01018
01019
01020 BOOL
01021 BITWISE_DCE::Redundant_cvtl(BOOL sign_xtd, INT32 to_bit, INT32 from_bit,
01022 CODEREP *opnd)
01023 {
01024 Is_True(to_bit == 32 || to_bit == 64,
01025 ("BITWISE_DCE::Redundant_cvtl: illegal to_bit"));
01026 Is_True(from_bit != 0,
01027 ("BITWISE_DCE::Redundant_cvtl: illegal from_bit"));
01028 MTYPE dtyp = opnd->Dtyp();
01029 if (dtyp == MTYPE_B)
01030 return ! sign_xtd || from_bit != 1;
01031 if (! MTYPE_is_integral(dtyp))
01032 return FALSE;
01033
01034 switch (opnd->Kind()) {
01035 case CK_CONST:
01036 if (Split_64_Bit_Int_Ops &&
01037 MTYPE_bit_size(opnd->Dtyp()) <= 32 &&
01038 to_bit > 32)
01039 return FALSE;
01040
01041 if (sign_xtd) {
01042 if (to_bit == 64) {
01043 INT64 sval64 = opnd->Const_val();
01044 sval64 = sval64 << (64 - from_bit) >> (64 - from_bit);
01045 return sval64 == opnd->Const_val();
01046 }
01047 else {
01048 INT32 sval32 = opnd->Const_val();
01049 return sval32 == (sval32 << (32 - from_bit) >> (32 - from_bit));
01050 }
01051 }
01052 else {
01053 if (to_bit == 64) {
01054 UINT64 uval64 = opnd->Const_val();
01055 uval64 = uval64 << (64 - from_bit) >> (64 - from_bit);
01056 return uval64 == opnd->Const_val();
01057 }
01058 else {
01059 UINT32 uval32 = opnd->Const_val();
01060 return uval32 == (uval32 << (32 - from_bit) >> (32 - from_bit));
01061 }
01062 }
01063
01064 case CK_RCONST:
01065 case CK_LDA:
01066 return FALSE;
01067
01068 case CK_VAR: {
01069 AUX_STAB_ENTRY *aux = Opt_stab()->Aux_stab_entry(opnd->Aux_id());
01070 if (aux->Is_dedicated_preg()) {
01071 #ifdef TARG_SL
01072 if ((sign_xtd == opnd->Is_sign_extd()) && (from_bit >= MTYPE_size_min(opnd->Dsctyp()))) {
01073 return TRUE;
01074 } else if (opnd->Defstmt() &&
01075 (opnd->Defstmt()->Opr() == OPR_INTRINSIC_CALL)) {
01076
01077 CODEREP *rhs = opnd->Defstmt()->Rhs();
01078 if (rhs && (rhs->Intrinsic() == INTRN_C3_SAVE_ACC)) {
01079 CODEREP *shr = rhs->Opnd(1);
01080 if ((shr->Kind() == CK_IVAR) && (shr->Ilod_base() != NULL)) {
01081 if ((shr->Ilod_base()->Kind() == CK_CONST) &&
01082 (shr->Ilod_base()->Const_val() == 16)) {
01083 return TRUE;
01084 }
01085 }
01086 return FALSE;
01087 }
01088 }
01089 #endif
01090 return FALSE;
01091 }
01092 if (ST_class(aux->St()) == CLASS_PREG) {
01093
01094 Is_True(! opnd->Is_flag_set(CF_DEF_BY_CHI) ||
01095
01096 opnd->Defstmt()->Opr() == OPR_OPT_CHI,
01097
01098 ("BITWISE_DCE::Redundant_cvtl: preg cannot be defined by chi"));
01099 if (opnd->Is_flag_set(CF_DEF_BY_PHI)) {
01100
01101
01102 return FALSE;
01103 }
01104 #ifdef TARG_X8664 // suppress this optimization since not yet in sync with
01105
01106 else if (MTYPE_size_min(dtyp) == 32)
01107 return FALSE;
01108 #endif
01109
01110 else if (opnd->Defstmt() &&
01111
01112 opnd->Defstmt()->Rhs())
01113
01114 return Redundant_cvtl(sign_xtd, to_bit, from_bit, opnd->Defstmt()->Rhs());
01115 else
01116 return FALSE;
01117 }
01118
01119 if (Split_64_Bit_Int_Ops && to_bit == 64)
01120 return FALSE;
01121 #ifdef TARG_X8664
01122 if (MTYPE_size_min(opnd->Dsctyp()) < 32 && to_bit == 64)
01123 return !sign_xtd && !opnd->Is_sign_extd() &&
01124 from_bit >= MTYPE_size_min(opnd->Dsctyp());
01125 #endif
01126 if (sign_xtd == opnd->Is_sign_extd())
01127 return from_bit >= MTYPE_size_min(opnd->Dsctyp());
01128 return ! opnd->Is_sign_extd() && from_bit > MTYPE_size_min(opnd->Dsctyp());
01129 }
01130
01131 case CK_IVAR:
01132 if (opnd->Opr() == OPR_PARM)
01133 return FALSE;
01134 if (opnd->Opr() == OPR_MLOAD)
01135 return FALSE;
01136 if (Split_64_Bit_Int_Ops && to_bit == 64)
01137 return FALSE;
01138 #ifdef TARG_X8664
01139 if (MTYPE_size_min(opnd->Dsctyp()) < 32 && to_bit == 64)
01140 return !sign_xtd && !opnd->Is_sign_extd() &&
01141 from_bit >= MTYPE_size_min(opnd->Dsctyp());
01142 #endif
01143 if (sign_xtd == opnd->Is_sign_extd())
01144 return from_bit >= MTYPE_size_min(opnd->Dsctyp());
01145 return ! opnd->Is_sign_extd() && from_bit > MTYPE_size_min(opnd->Dsctyp());
01146
01147 case CK_OP: {
01148 MTYPE dsctyp;
01149 OPERATOR opr = opnd->Opr();
01150 switch(opr) {
01151
01152 case OPR_CVTL:
01153 #ifdef KEY
01154
01155
01156
01157
01158 if (opnd->Usecnt() > 0 &&
01159 (Livebits(opnd) & ~Bitmask_of_size(opnd->Offset())) == 0)
01160 return FALSE;
01161 #endif
01162 if (MTYPE_is_signed(dtyp) == sign_xtd) {
01163 #ifndef TARG_X8664
01164 return from_bit >= opnd->Offset();
01165 #else
01166 if (! MTYPE_is_signed(dtyp))
01167 return from_bit >= opnd->Offset();
01168 else {
01169 INT32 cvtl_to_bit = MTYPE_size_min(dtyp);
01170 return from_bit >= opnd->Offset() && to_bit <= cvtl_to_bit;
01171 }
01172 #endif
01173 }
01174 return ! MTYPE_is_signed(dtyp) && from_bit > opnd->Offset();
01175
01176 case OPR_CVT:
01177 dsctyp = opnd->Dsctyp();
01178 if (! MTYPE_is_integral(dsctyp) ||
01179 MTYPE_size_min(dtyp) <= MTYPE_size_min(dsctyp))
01180 return FALSE;
01181 #ifdef KEY
01182 if (MTYPE_size_min(dtyp) > MTYPE_size_min(dsctyp) &&
01183 opnd->Usecnt() > 0 &&
01184 (Livebits(opnd) & ~Bits_in_type(dsctyp)) == 0)
01185 return FALSE;
01186 #endif
01187 return MTYPE_is_signed(dtyp) == sign_xtd &&
01188 from_bit >= MTYPE_size_min(dsctyp);
01189
01190 case OPR_EQ: case OPR_NE:
01191 case OPR_GE: case OPR_GT: case OPR_LE: case OPR_LT:
01192 case OPR_LNOT:
01193 case OPR_LAND: case OPR_LIOR:
01194 return ! sign_xtd || from_bit != 1;
01195 #ifdef KEY
01196 case OPR_BAND:
01197 { CODEREP *kopnd;
01198 if (sign_xtd)
01199 return FALSE;
01200 if (opnd->Opnd(0)->Kind() == CK_CONST)
01201 kopnd = opnd->Opnd(0);
01202 else if (opnd->Opnd(1)->Kind() == CK_CONST)
01203 kopnd = opnd->Opnd(1);
01204 else return FALSE;
01205 UINT64 uval64 = kopnd->Const_val();
01206 return uval64 <= ((0x1ll << from_bit) - 1);
01207 }
01208 #endif
01209 #ifdef TARG_SL
01210 case OPR_ASHR:
01211
01212
01213
01214
01215 if (opnd->Opnd(1)->Kind() == CK_CONST) {
01216 if (from_bit >= (MTYPE_size_min(dtyp) - opnd->Opnd(1)->Const_val())) {
01217 return (MTYPE_signed(dtyp) == sign_xtd);
01218 }
01219 }
01220 return FALSE;
01221
01222 case OPR_INTRINSIC_OP:
01223
01224 if ((from_bit == 16) && (to_bit == 32)) {
01225 return (MTYPE_signed(dtyp) == sign_xtd);
01226 }
01227 return FALSE;
01228
01229 case OPR_EXTRACT_BITS:
01230
01231
01232
01233 if (opnd->Op_bit_size() <= from_bit) {
01234 if (MTYPE_signed(dtyp) == sign_xtd)
01235 return TRUE;
01236 if (opnd->Op_bit_size() < from_bit)
01237 return ! MTYPE_signed(dtyp);
01238 }
01239 return FALSE;
01240 #endif
01241 default: ;
01242 }
01243 return FALSE;
01244 }
01245
01246 default: ;
01247 }
01248 return FALSE;
01249 }
01250
01251
01252
01253
01254
01255 CODEREP *
01256 BITWISE_DCE::Copy_propagate(CODEREP *cr, STMTREP *use_stmt) {
01257 if (Usecnt(cr) != 1
01258 || cr->Is_flag_set((CR_FLAG)(CF_DEF_BY_PHI | CF_DEF_BY_CHI))
01259 || cr->Defstmt() == NULL)
01260 return NULL;
01261 Is_True(cr->Defstmt()->Opr() == OPR_STID,
01262 ("BITWISE_DCE::Copy_propagate: cr->Defstmt()->Opr() != OPR_STID"));
01263
01264 #ifndef KEY
01265
01266 if (use_stmt->Prev() != cr->Defstmt())
01267 return NULL;
01268 #else
01269
01270 if (use_stmt->Prev() != cr->Defstmt()) {
01271 if (WOPT_Enable_Bdceprop_Limit != -1 &&
01272 use_stmt->Bb()->Id() > WOPT_Enable_Bdceprop_Limit)
01273 return NULL;
01274 else if (! (Opt_stab()->Aux_stab_entry(cr->Aux_id())->EPRE_temp() &&
01275 use_stmt->Bb() == cr->Defstmt()->Bb() &&
01276 !use_stmt->Iv_update() ) )
01277 return NULL;
01278 }
01279 #endif
01280
01281 #ifndef TARG_NVISA
01282 #ifdef KEY // bug 8335: this may prevent CG from knowing what register name
01283
01284 if (use_stmt->Opr() == OPR_ASM_STMT &&
01285 ST_class(Opt_stab()->St(cr->Aux_id())) == CLASS_PREG)
01286 return NULL;
01287 #endif
01288 #endif
01289
01290 CODEREP *new_expr = cr->Defstmt()->Rhs();
01291 Is_True(new_expr != NULL,
01292 ("BITWISE_DCE::Copy_propagate: new_expr = NULL"));
01293
01294 if (! new_expr->Propagatable_for_ivr(Opt_stab()))
01295 return NULL;
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306 if (Tracing()) {
01307 fprintf(TFile, "BDCE copying:\n");
01308 cr->Defstmt()->Print(TFile);
01309 fprintf(TFile, "to:\n");
01310 use_stmt->Print(TFile);
01311 }
01312
01313 new_expr->IncUsecnt_rec();
01314 use_stmt->Bb()->Remove_stmtrep(cr->Defstmt());
01315 return new_expr;
01316 }
01317
01318
01319
01320
01321
01322 CODEREP *
01323 BITWISE_DCE::Delete_cvtls(CODEREP *cr, STMTREP *use_stmt)
01324 {
01325 if (MTYPE_is_integral(cr->Dtyp()) && Livebits(cr) == 0
01326 #ifdef KEY // bug 14142
01327 && ! cr->Has_volatile_content()
01328 #endif
01329 ) {
01330
01331 cr->DecUsecnt_rec();
01332 return Htable()->Add_const(cr->Dtyp(), 0);
01333 }
01334
01335 CODEREP *x, *x2;
01336 CODEREP *new_cr = Alloc_stack_cr(cr->Extra_ptrs_used());
01337 BOOL need_rehash;
01338 INT32 i;
01339 OPERATOR opr;
01340 switch (cr->Kind()) {
01341 case CK_CONST:
01342 case CK_RCONST:
01343 case CK_LDA:
01344 return NULL;
01345 case CK_VAR:
01346 return ( _copy_propagate ? Copy_propagate(cr, use_stmt) : NULL );
01347 case CK_IVAR:
01348 x = Delete_cvtls(cr->Ilod_base(), use_stmt);
01349 if (cr->Opr() == OPR_MLOAD)
01350 x2 = Delete_cvtls(cr->Mload_size(), use_stmt);
01351 else if (cr->Opr() == OPR_ILOADX)
01352 x2 = Delete_cvtls(cr->Index(), use_stmt);
01353 else x2 = NULL;
01354 if (x || x2) {
01355 new_cr->Copy(*cr);
01356 new_cr->Set_istr_base(NULL);
01357 new_cr->Set_usecnt(0);
01358 if (x)
01359 new_cr->Set_ilod_base(x);
01360 if (x2)
01361 new_cr->Set_mload_size(x2);
01362 new_cr->Set_ivar_occ(cr->Ivar_occ());
01363 cr->DecUsecnt();
01364 return Htable()->Rehash(new_cr);
01365 }
01366 return NULL;
01367
01368 case CK_OP:
01369 need_rehash = FALSE;
01370 new_cr->Copy(*cr);
01371 new_cr->Set_usecnt(0);
01372
01373 for (i = 0; i < cr->Kid_count(); i++) {
01374 x = Delete_cvtls(cr->Opnd(i), use_stmt);
01375 if (x) {
01376 need_rehash = TRUE;
01377 new_cr->Set_opnd(i, x);
01378 }
01379 else new_cr->Set_opnd(i, cr->Opnd(i));
01380 }
01381
01382 opr = cr->Opr();
01383 if (opr == OPR_CVTL) {
01384 if (((Livebits(cr) & ~Bitmask_of_size(cr->Offset())) == 0) ||
01385 Redundant_cvtl(MTYPE_is_signed(cr->Dtyp()),
01386 MTYPE_size_min(cr->Dtyp()), cr->Offset(),
01387 #ifdef KEY
01388 x ? x :
01389 #endif
01390 cr->Opnd(0))) {
01391 if (Tracing()) fprintf(TFile, "delete cvtl (cr%d)\n", cr->Coderep_id());
01392
01393 cr->DecUsecnt();
01394 if (need_rehash)
01395 return x;
01396 else return cr->Opnd(0);
01397 }
01398 }
01399 else if (opr == OPR_CVT) {
01400
01401
01402 #if !defined(TARG_IA32) && !defined(TARG_NVISA) && !defined(TARG_SL)
01403 MTYPE dtyp = cr->Dtyp();
01404 MTYPE dsctyp = cr->Dsctyp();
01405 if (dsctyp == MTYPE_B)
01406 ;
01407
01408
01409
01410
01411 else if(MTYPE_is_vector(dtyp) || MTYPE_is_vector(dsctyp))
01412 ;
01413 else if (MTYPE_is_integral(dtyp) && MTYPE_is_integral(dsctyp)) {
01414 if (MTYPE_size_min(dtyp) > MTYPE_size_min(dsctyp)) {
01415 if (((Livebits(cr) & ~Bits_in_type(dsctyp)) == 0) ||
01416 Redundant_cvtl(MTYPE_is_signed(dsctyp),
01417 MTYPE_size_min(dtyp), MTYPE_size_min(dsctyp),
01418 #ifdef KEY
01419 x ? x :
01420 #endif
01421 cr->Opnd(0))) {
01422
01423 cr->DecUsecnt();
01424 if (need_rehash)
01425 return x;
01426 else return cr->Opnd(0);
01427 }
01428 }
01429 #if !defined(TARG_MIPS) && !defined(TARG_X8664) && !defined(TARG_NVISA)
01430
01431 else {
01432 if ((Livebits(cr) & ~Bitmask_of_size(MTYPE_size_min(dtyp))) == 0) {
01433
01434 cr->DecUsecnt();
01435 if (need_rehash)
01436 return x;
01437 else return cr->Opnd(0);
01438 }
01439 }
01440 #endif
01441 }
01442 #endif
01443 }
01444 #ifdef TARG_X8664
01445 else if (! Is_Target_64bit() && MTYPE_size_min(cr->Dtyp()) == 64 &&
01446 MTYPE_is_integral(cr->Dtyp()) &&
01447 (Livebits(cr) >> 32) == 0 &&
01448 (opr == OPR_NEG || opr == OPR_BNOT || opr == OPR_LNOT ||
01449 opr == OPR_ADD || opr == OPR_SUB || opr == OPR_MPY ||
01450 opr == OPR_BAND || opr == OPR_BIOR || opr == OPR_BNOR ||
01451 opr == OPR_BXOR || opr == OPR_LAND || opr == OPR_LIOR)) {
01452
01453 cr->Set_dtyp(Mtype_TransferSize(MTYPE_I4, cr->Dtyp()));
01454 if (cr->Dsctyp() != MTYPE_V)
01455 cr->Set_dsctyp(Mtype_TransferSize(MTYPE_I4, cr->Dsctyp()));
01456 new_cr->Set_dtyp(Mtype_TransferSize(MTYPE_I4, new_cr->Dtyp()));
01457 if (new_cr->Dsctyp() != MTYPE_V)
01458 new_cr->Set_dsctyp(Mtype_TransferSize(MTYPE_I4, new_cr->Dsctyp()));
01459 }
01460 #endif
01461
01462
01463 if (need_rehash) {
01464 cr->DecUsecnt();
01465 return Htable()->Rehash(new_cr);
01466 }
01467 return NULL;
01468
01469 default: ;
01470 }
01471 return NULL;
01472 }
01473
01474
01475
01476
01477
01478
01479 void
01480 BITWISE_DCE::Delete_dead_nodes(void)
01481 {
01482 INT32 i;
01483 CFG_ITER cfg_iter(Cfg());
01484 BB_NODE *bb;
01485
01486 FOR_ALL_NODE( bb, cfg_iter, Init() ) {
01487
01488 STMTREP *stmt, *nextstmt;
01489 for (stmt = bb->First_stmtrep();
01490 stmt != NULL;
01491 stmt = nextstmt) {
01492 nextstmt = stmt->Next();
01493 if (! stmt->Live_stmt() &&
01494 stmt->Opr() == OPR_STID && Livebits(stmt->Lhs()) != 0)
01495 stmt->Set_live_stmt();
01496 if (! stmt->Live_stmt()) {
01497 #if Is_True_On // verify that there is no live chi
01498 CHI_LIST_ITER chi_iter;
01499 CHI_NODE *chi;
01500 FOR_ALL_NODE(chi, chi_iter, Init(stmt->Chi_list()))
01501 Is_True(! chi->Live() ||
01502 chi->RESULT()->Is_flag_set(CF_IS_ZERO_VERSION) ||
01503 Livebits(chi->RESULT()) == 0,
01504 ("BITWISE_DCE::Delete_dead_nodes: live chi in dead stmt"));
01505 #endif // Is_True_On
01506 bb->Remove_stmtrep(stmt);
01507 continue;
01508 }
01509
01510
01511 OPERATOR opr = stmt->Opr();
01512 CODEREP *rhs = stmt->Rhs();
01513 CODEREP *x;
01514 if (OPERATOR_is_call(opr) || opr == OPR_ASM_STMT) {
01515 for (i = 0; i < rhs->Kid_count(); i++) {
01516 x = Delete_cvtls(rhs->Opnd(i), stmt);
01517 if (x)
01518 rhs->Set_opnd(i, x);
01519 }
01520 continue;
01521 }
01522 if (rhs) {
01523 if (opr == OPR_PREFETCH) {
01524 x = Delete_cvtls(rhs->Ilod_base(), stmt);
01525 if (x)
01526 rhs->Set_ilod_base(x);
01527 }
01528 else {
01529 x = Delete_cvtls(rhs, stmt);
01530 if (x)
01531 stmt->Set_rhs(x);
01532 }
01533 }
01534 if (OPERATOR_is_store(opr)) {
01535 CODEREP *lhs = stmt->Lhs();
01536 switch (opr) {
01537 case OPR_MSTORE:
01538 x = Delete_cvtls(lhs->Mstore_size(), stmt);
01539 if (x)
01540 lhs->Set_mstore_size(x);
01541
01542 case OPR_ISTORE:
01543 x = Delete_cvtls(lhs->Istr_base(), stmt);
01544 if (x)
01545 lhs->Set_istr_base(x);
01546 break;
01547 default: ;
01548 }
01549 }
01550 }
01551 }
01552 }
01553
01554 #if defined(TARG_SL)
01555 void
01556 BITWISE_DCE::Repair_Injured_AuxIntrnOP(void) {
01557 CFG_ITER cfg_iter(Cfg());
01558 BB_NODE *bb;
01559 FOR_ALL_NODE( bb, cfg_iter, Init() ) {
01560 STMTREP_ITER stmt_iter(bb->Stmtlist());
01561 STMTREP *stmt;
01562 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
01563 if (stmt->Live_stmt())
01564 continue;
01565 if (stmt->Opr() == OPR_STID) {
01566 if (CR_Intrinsic_Op_Slave(stmt->Rhs())) {
01567 CODEREP *parm2cr = stmt->Rhs()->Opnd(0);
01568 Is_True(parm2cr->Kind() == CK_IVAR, ("kid of intrinsic op is parm"));
01569 CODEREP *op2cr = parm2cr->Ilod_base();
01570 if (op2cr && (Livebits(op2cr) > 0))
01571 Mark_entire_var_live(stmt->Lhs(), FALSE);
01572 }
01573 }
01574 }
01575 }
01576 return;
01577 }
01578 #endif
01579
01580
01581
01582
01583 void
01584 BITWISE_DCE::Bitwise_dce(void)
01585 {
01586 Initialize_stmts_dead();
01587
01588
01589 CFG_ITER cfg_iter(Cfg());
01590 BB_NODE *bb;
01591 FOR_ALL_NODE( bb, cfg_iter, Init() )
01592 Make_bb_live(bb);
01593
01594 if (Cfg()->Fake_exit_bb() == NULL) {
01595 Find_and_mark_return_live(Cfg()->Exit_bb());
01596 }
01597 else {
01598 BB_NODE *pdom_bb;
01599 BB_LIST_ITER pdom_bb_iter;
01600 FOR_ALL_ELEM(pdom_bb, pdom_bb_iter, Init(Cfg()->Exit_bb()->Pdom_bbs()))
01601 if (pdom_bb->Willexit())
01602 Find_and_mark_return_live(pdom_bb);
01603 else Mark_willnotexit_stmts_live(pdom_bb);
01604 }
01605
01606 #if defined(TARG_SL)
01607 Repair_Injured_AuxIntrnOP();
01608 #endif
01609
01610
01611 #if !defined(KEY) || defined(TARG_NVISA) // bug 8499
01612
01613 if ( _copy_propagate ) {
01614 FOR_ALL_NODE( bb, cfg_iter, Init() ) {
01615 STMTREP_ITER stmt_iter(bb->Stmtlist());
01616 STMTREP *stmt;
01617 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
01618 if (! stmt->Live_stmt() && stmt->Opr() == OPR_STID
01619 && Livebits(stmt->Lhs()) != 0)
01620 Mark_tree_bits_live(stmt->Rhs(), Livebits(stmt->Lhs()),
01621 TRUE );
01622 }
01623 }
01624 }
01625 #endif
01626
01627 if (Tracing()) {
01628 Print_nodes_with_dead_bits(TFile);
01629 Print_node_usecnts(TFile);
01630 }
01631
01632
01633 Delete_dead_nodes();
01634 }
01635
01636
01637
01638
01639 void
01640 COMP_UNIT::Do_bitwise_dce(BOOL copy_propagate)
01641 {
01642 MEM_POOL bdce_pool;
01643
01644 if ( Get_Trace(TP_GLOBOPT, DCE_DUMP_FLAG)) {
01645 fprintf( TFile, "%sBefore COMP_UNIT::Do_bitwise_dce\n%s",
01646 DBar, DBar );
01647 Cfg()->Print(TFile);
01648 }
01649
01650 OPT_POOL_Initialize(&bdce_pool, "bitwise dce pool", FALSE, DCE_DUMP_FLAG);
01651 OPT_POOL_Push(&bdce_pool, DCE_DUMP_FLAG);
01652
01653 {
01654 BITWISE_DCE bitwise_dce(Htable(), Opt_stab(), Cfg(), &bdce_pool,
01655 copy_propagate);
01656 bitwise_dce.Bitwise_dce();
01657 }
01658
01659 OPT_POOL_Pop(&bdce_pool, DCE_DUMP_FLAG);
01660 OPT_POOL_Delete(&bdce_pool, DCE_DUMP_FLAG);
01661
01662 if ( Get_Trace(TP_GLOBOPT, DCE_DUMP_FLAG)) {
01663 fprintf( TFile, "%sAfter COMP_UNIT::Do_bitwise_dce\n%s",
01664 DBar, DBar );
01665 Cfg()->Print(TFile);
01666 }
01667 }
01668
01669
01670
01671
01672 void
01673 BITWISE_DCE::Print_nodes_with_dead_bits(FILE *fp)
01674 {
01675 CODEREP_ITER cr_iter;
01676 CODEREP *cr, *bucket;
01677 CODEMAP_ITER codemap_iter;
01678
01679 fprintf(fp, "%sBitwise DCE found following nodes with dead bits in PU %s\n%s",
01680 DBar, Cur_PU_Name, DBar);
01681
01682 fprintf(fp, "- - - Default vsym is sym%1d\n", Opt_stab()->Default_vsym());
01683 fprintf(fp, "- - - Return vsym is sym%1d\n", Opt_stab()->Return_vsym());
01684
01685
01686 FOR_ALL_ELEM(bucket, codemap_iter, Init(Htable())) {
01687 FOR_ALL_NODE(cr, cr_iter, Init(bucket)) {
01688 if (cr->Dtyp() != MTYPE_UNKNOWN &&
01689 Livebits(cr) != Bits_in_coderep_result(cr)) {
01690 if (cr->Kind() == CK_OP) {
01691 switch (cr->Opr()) {
01692 case OPR_EQ: case OPR_NE:
01693 case OPR_GE: case OPR_GT: case OPR_LE: case OPR_LT:
01694 case OPR_LNOT:
01695 case OPR_LAND: case OPR_LIOR:
01696 if (Livebits(cr) != 0)
01697 continue;
01698 default: ;
01699 }
01700 }
01701 else if (cr->Kind() == CK_IVAR)
01702 continue;
01703 Htable()->Print_CR(cr, fp);
01704 fprintf(fp, " has live bits 0x%llx\n", Livebits(cr));
01705 }
01706 }
01707 }
01708
01709
01710 AUX_ID i;
01711 AUX_STAB_ITER aux_stab_iter(Opt_stab());
01712 FOR_ALL_NODE(i, aux_stab_iter, Init()) {
01713 AUX_STAB_ENTRY *aux = Opt_stab()->Aux_stab_entry(i);
01714 FOR_ALL_NODE(cr, cr_iter, Init(aux->Cr_list())) {
01715 if (Livebits(cr) != Bits_in_var(cr) &&
01716 ! cr->Is_flag_set(CF_IS_ZERO_VERSION) &&
01717 ! Opt_stab()->Aux_stab_entry(cr->Aux_id())->Is_dedicated_preg()) {
01718 Htable()->Print_CR(cr, fp);
01719 fprintf(fp, " has live bits 0x%llx\n", Livebits(cr));
01720 }
01721 }
01722 }
01723
01724 fprintf(fp, "%sBitwise DCE found following stmts dead in PU %s\n%s",
01725 DBar, Cur_PU_Name, DBar);
01726 CFG_ITER cfg_iter(Cfg());
01727 BB_NODE *bb;
01728
01729 FOR_ALL_NODE( bb, cfg_iter, Init() ) {
01730
01731 STMTREP_ITER stmt_iter(bb->Stmtlist());
01732 STMTREP *stmt;
01733 FOR_ALL_NODE(stmt, stmt_iter, Init()) {
01734 if (stmt->Live_stmt())
01735 continue;
01736 if (stmt->Opr() == OPR_STID && Livebits(stmt->Lhs()) != 0)
01737 continue;
01738 stmt->Print(fp);
01739 }
01740 }
01741 }
01742
01743
01744
01745
01746 void
01747 BITWISE_DCE::Print_node_usecnts(FILE *fp)
01748 {
01749 CODEREP_ITER cr_iter;
01750 CODEREP *cr, *bucket;
01751 CODEMAP_ITER codemap_iter;
01752
01753 fprintf(fp, "%sBitwise DCE counted the following variable uses in PU %s\n%s",
01754 DBar, Cur_PU_Name, DBar);
01755
01756
01757 AUX_ID i;
01758 AUX_STAB_ITER aux_stab_iter(Opt_stab());
01759 FOR_ALL_NODE(i, aux_stab_iter, Init()) {
01760 AUX_STAB_ENTRY *aux = Opt_stab()->Aux_stab_entry(i);
01761 FOR_ALL_NODE(cr, cr_iter, Init(aux->Cr_list())) {
01762 fprintf(fp, "cr%d has %u uses\n", cr->Coderep_id(), Usecnt(cr));
01763 }
01764 }
01765 }
01766
01767
01768
01769
01770 void
01771 BITWISE_DCE::Print_livebits(INT32 cr_id)
01772 {
01773 fprintf(TFile, "0x%llx\n", _livebits[cr_id]);
01774 }
01775
01776