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 #ifdef USE_PCH
00076 #include "lno_pch.h"
00077 #endif // USE_PCH
00078 #pragma hdrstop
00079
00080 const static char *source_file = __FILE__;
00081 const static char *rcs_id = "$Source$ $Revision$";
00082
00083 #include <sys/types.h>
00084 #include "lnopt_main.h"
00085 #include "dep_graph.h"
00086 #include "lwn_util.h"
00087 #include "opt_du.h"
00088 #include "reduc.h"
00089 #include "config.h"
00090 #include "targ_const.h"
00091
00092 static BOOL Process_If(WN *if_wn, ARRAY_DIRECTED_GRAPH16 *dep_graph);
00093 static BOOL Is_Zero(WN *wn);
00094 static BOOL Equivalent_Load(WN *wn1, WN *wn2, WN *statement,
00095 ARRAY_DIRECTED_GRAPH16 *dep_graph);
00096 static WN *Get_Single_Real_Statement(WN *block);
00097 static BOOL Block_Is_Empty(WN *block);
00098
00099 void Eliminate_Zero_Mult(WN *wn, ARRAY_DIRECTED_GRAPH16 *dep_graph)
00100 {
00101 Is_True(Eager_Level >= 4,("Eliminate_Zero_Mult causes speculation "));
00102 OPCODE opcode = WN_opcode(wn);
00103
00104 if (opcode == OPC_BLOCK) {
00105 WN *kid = WN_first(wn);
00106 while (kid) {
00107 Eliminate_Zero_Mult(kid,dep_graph);
00108 kid = WN_next(kid);
00109 }
00110 return;
00111 }
00112
00113 if (opcode == OPC_IF) {
00114 if (Process_If(wn,dep_graph)) {
00115 return;
00116 }
00117 }
00118
00119 for (INT kidno=0; kidno<WN_kid_count(wn); kidno++) {
00120 WN *kid = WN_kid(wn,kidno);
00121 Eliminate_Zero_Mult(kid,dep_graph);
00122 }
00123 }
00124
00125
00126
00127 static BOOL Process_If(WN *if_wn, ARRAY_DIRECTED_GRAPH16 *dep_graph)
00128 {
00129 WN *parent = LWN_Get_Parent(if_wn);
00130
00131
00132 if ((WN_opcode(parent) != OPC_BLOCK) ||
00133 (WN_opcode(LWN_Get_Parent(parent)) != OPC_DO_LOOP)) {
00134 return FALSE;
00135 }
00136 if (!Do_Loop_Is_Good(LWN_Get_Parent(parent)) ||
00137 Do_Loop_Has_Gotos(LWN_Get_Parent(parent))) {
00138 return FALSE;
00139 }
00140
00141 WN *test = WN_if_test(if_wn);
00142 OPERATOR oper = WN_operator(test);
00143 WN *multiplier = NULL;
00144 WN *loop = NULL;
00145
00146 if (oper == OPR_NE) {
00147 if (Is_Zero(WN_kid0(test))) {
00148 multiplier = WN_kid1(test);
00149 } else if (Is_Zero(WN_kid1(test))) {
00150 multiplier = WN_kid0(test);
00151 } else {
00152 return FALSE;
00153 }
00154
00155
00156 if (!Block_Is_Empty(WN_else(if_wn))) {
00157 return FALSE;
00158 }
00159 loop = Get_Single_Real_Statement(WN_then(if_wn));
00160 if (!loop || WN_opcode(loop) != OPC_DO_LOOP) {
00161 return FALSE;
00162 }
00163 if (!Do_Loop_Is_Good(loop) || Do_Loop_Has_Gotos(loop)) {
00164 return FALSE;
00165 }
00166 } else if (oper == OPR_EQ) {
00167 if (Is_Zero(WN_kid0(test))) {
00168 multiplier = WN_kid1(test);
00169 } else if (Is_Zero(WN_kid1(test))) {
00170 multiplier = WN_kid0(test);
00171 } else {
00172 return FALSE;
00173 }
00174
00175 if (!Block_Is_Empty(WN_then(if_wn))) {
00176 return FALSE;
00177 }
00178 loop = Get_Single_Real_Statement(WN_else(if_wn));
00179 if (!loop || WN_opcode(loop) != OPC_DO_LOOP) {
00180 return FALSE;
00181 }
00182 if (!Do_Loop_Is_Good(loop) || Do_Loop_Has_Gotos(loop)) {
00183 return FALSE;
00184 }
00185 } else {
00186 return FALSE;
00187 }
00188
00189
00190
00191
00192 if (!OPCODE_is_load(WN_opcode(multiplier))) {
00193 return FALSE;
00194 }
00195
00196
00197
00198 WN *body = WN_do_body(loop);
00199 WN *statement = Get_Single_Real_Statement(body);
00200 if (!statement || !OPCODE_is_store(WN_opcode(statement))) {
00201 return FALSE;
00202 }
00203
00204 if (!red_manager || (red_manager->Which_Reduction(statement) != RED_ADD)) {
00205 return FALSE;
00206 }
00207
00208 WN *add = WN_kid0(statement);
00209 Is_True((WN_operator(add) == OPR_ADD) ||
00210 (WN_operator(add) == OPR_SUB),
00211 ("Non add in Process_If"));
00212 WN *loop_mult = NULL;
00213 if (WN_operator(WN_kid0(add)) == OPR_MPY) {
00214 loop_mult = WN_kid0(add);
00215 } else if (WN_operator(WN_kid1(add)) == OPR_MPY) {
00216 loop_mult = WN_kid1(add);
00217 } else {
00218 return FALSE;
00219 }
00220
00221 if (Equivalent_Load(multiplier,WN_kid0(loop_mult),statement,dep_graph) ||
00222 Equivalent_Load(multiplier,WN_kid1(loop_mult),statement,dep_graph)) {
00223
00224 LWN_Insert_Block_Before(parent,if_wn,LWN_Extract_From_Block(loop));
00225 LWN_Delete_Tree(if_wn);
00226 return TRUE;
00227 }
00228 return FALSE;
00229 }
00230
00231
00232
00233 static BOOL Is_Zero(WN *wn)
00234 {
00235 OPERATOR oper = WN_operator(wn);
00236 if (oper == OPR_INTCONST) {
00237 return (WN_const_val(wn) == 0);
00238 } else if (oper == OPR_CONST) {
00239 TCON t = STC_val(WN_st(wn));
00240 switch(TCON_ty(t)) {
00241 case MTYPE_F4:
00242 #ifdef TCON_R4_IS_DOUBLE
00243 return t.vals.dval == 0;
00244 #else
00245 return t.vals.fval == 0;
00246 #endif
00247 case MTYPE_F8:
00248 return t.vals.dval == 0;
00249 default:
00250 return FALSE;
00251 }
00252 }
00253 return FALSE;
00254 }
00255
00256
00257
00258
00259
00260 static BOOL Equivalent_Load(WN *wn1, WN *wn2, WN *statement,
00261 ARRAY_DIRECTED_GRAPH16 *dep_graph)
00262 {
00263 OPCODE opcode1 = WN_opcode(wn1);
00264 OPCODE opcode2 = WN_opcode(wn2);
00265
00266 if (opcode1 != opcode2) {
00267 return FALSE;
00268 }
00269 if (!OPCODE_is_load(opcode1)) {
00270 return FALSE;
00271 }
00272
00273 if (OPCODE_operator(opcode1) == OPR_LDA) {
00274 return ((WN_offset(wn1) == WN_offset(wn2)) &&
00275 (ST_base(WN_st(wn1)) == ST_base(WN_st(wn2))) &&
00276 (ST_ofst(WN_st(wn1)) == ST_ofst(WN_st(wn2))));
00277 } else if (OPCODE_operator(opcode1) == OPR_LDID) {
00278 if ((WN_offset(wn1) != WN_offset(wn2)) ||
00279 (ST_base(WN_st(wn1)) != ST_base(WN_st(wn2))) ||
00280 (ST_ofst(WN_st(wn1)) != ST_ofst(WN_st(wn2)))) {
00281 return FALSE;
00282 }
00283
00284
00285 DEF_LIST *defs = Du_Mgr->Ud_Get_Def(wn2);
00286 DEF_LIST_ITER iter(defs);
00287 if (defs->Incomplete()) return FALSE;
00288 for(const DU_NODE *node=iter.First();!iter.Is_Empty();node=iter.Next()){
00289 WN *def = (WN *) node->Wn();
00290 if (def == statement) {
00291 return FALSE;
00292 }
00293 }
00294 } else if (OPCODE_operator(opcode1) == OPR_ILOAD) {
00295 WN *array1 = WN_kid0(wn1);
00296 WN *array2 = WN_kid0(wn2);
00297
00298
00299 ACCESS_ARRAY *aa1 = (ACCESS_ARRAY *) WN_MAP_Get(LNO_Info_Map,array1);
00300 ACCESS_ARRAY *aa2 = (ACCESS_ARRAY *) WN_MAP_Get(LNO_Info_Map,array2);
00301 if (!aa1 || !aa2) {
00302 return FALSE;
00303 }
00304 if (!(*aa1 == *aa2)) {
00305 return FALSE;
00306 }
00307
00308 VINDEX16 v1 = dep_graph->Get_Vertex(wn1);
00309 VINDEX16 v2 = dep_graph->Get_Vertex(wn2);
00310 if (!v1 || !v2) {
00311 return FALSE;
00312 }
00313
00314
00315
00316 VINDEX16 store_v = dep_graph->Get_Vertex(statement);
00317 if (store_v && dep_graph->Get_Edge(store_v,v2)) {
00318 return FALSE;
00319 }
00320 } else {
00321 return FALSE;
00322 }
00323 return TRUE;
00324 }
00325
00326
00327
00328
00329 static WN *Get_Single_Real_Statement(WN *block)
00330 {
00331 INT count = 0;
00332 WN *statement = NULL;
00333 WN *tmp = WN_first(block);
00334 while (tmp) {
00335 if (!OPCODE_is_not_executable(WN_opcode(tmp))) {
00336 count++;
00337 if (count > 1) {
00338 return NULL;
00339 }
00340 statement = tmp;
00341 }
00342 tmp = WN_next(tmp);
00343 }
00344 return (statement);
00345 }
00346
00347
00348 static BOOL Block_Is_Empty(WN *block)
00349 {
00350 WN *tmp = WN_first(block);
00351 while (tmp) {
00352 if (!OPCODE_is_not_executable(WN_opcode(tmp))) {
00353 return FALSE;
00354 }
00355 tmp = WN_next(tmp);
00356 }
00357 return TRUE;
00358 }
00359