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 #ifdef USE_PCH
00041 #include "lno_pch.h"
00042 #endif // USE_PCH
00043 #pragma hdrstop
00044
00045 #include <sys/types.h>
00046
00047 #include "lnopt_main.h"
00048 #include "config.h"
00049 #include "config_lno.h"
00050 #include "strtab.h"
00051 #include "stab.h"
00052 #include "targ_const.h"
00053
00054 #include "lnoutils.h"
00055 #include "wn_simp.h"
00056 #include "stdlib.h"
00057 #include "lwn_util.h"
00058 #include "optimizer.h"
00059 #include "opt_du.h"
00060 #include "name.h"
00061 #include "forward.h"
00062
00063 static DU_MANAGER* du = NULL;
00064
00065
00066
00067
00068
00069
00070
00071 static BOOL Matching_Stores(WN* wn_store_one,
00072 WN* wn_store_two)
00073 {
00074 OPCODE op_one = WN_opcode(wn_store_one);
00075 OPCODE op_two = WN_opcode(wn_store_two);
00076 if (op_one != op_two)
00077 return FALSE;
00078 OPERATOR opr = WN_operator(wn_store_one);
00079 if (opr == OPR_STID)
00080 return SYMBOL(wn_store_one) == SYMBOL(wn_store_two);
00081 if (opr == OPR_ISTORE) {
00082 WN* wn_array_one = WN_kid1(wn_store_one);
00083 if (WN_operator(wn_array_one) != OPR_ARRAY)
00084 return FALSE;
00085 WN* wn_array_two = WN_kid1(wn_store_two);
00086 if (WN_operator(wn_array_two) != OPR_ARRAY)
00087 return FALSE;
00088 WN* wn_base_one = WN_array_base(wn_array_one);
00089 WN* wn_base_two = WN_array_base(wn_array_two);
00090 ST* st_base_one(Get_ST_Base(wn_base_one));
00091 ST* st_base_two(Get_ST_Base(wn_base_two));
00092 BOOL same_base = (st_base_one == NULL || st_base_two == NULL)
00093 ? st_base_one == st_base_two
00094 : ST_base(st_base_one) == ST_base(st_base_two)
00095 && ST_ofst(st_base_one) == ST_ofst(st_base_two);
00096 if (!same_base)
00097 return FALSE;
00098 ACCESS_ARRAY* aa_one = (ACCESS_ARRAY*) WN_MAP_Get(LNO_Info_Map,
00099 wn_array_one);
00100 ACCESS_ARRAY* aa_two = (ACCESS_ARRAY*) WN_MAP_Get(LNO_Info_Map,
00101 wn_array_two);
00102 if (!(*aa_one == *aa_two))
00103 return FALSE;
00104 return TRUE;
00105 }
00106 return FALSE;
00107 }
00108
00109
00110
00111
00112
00113
00114 static WN* Store_Expr(WN* wn_store)
00115 {
00116 switch (WN_operator(wn_store)) {
00117 case OPR_STID:
00118 return WN_kid0(wn_store);
00119 case OPR_ISTORE:
00120 #ifdef KEY // Bug 2096
00121 return WN_kid0(wn_store);
00122 #else
00123 return WN_kid1(wn_store);
00124 #endif
00125 default:
00126 FmtAssert(TRUE, ("Store_Expr(): Don't understand this store type"));
00127 return NULL;
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 static BOOL Matching_Exprs(WN* wn_one,
00138 WN* wn_two)
00139 {
00140 return WN_Simp_Compare_Trees(wn_one, wn_two) == 0;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 static BOOL IFMM_Convertible(WN* wn_tree,
00152 BOOL* if_mm_max)
00153 {
00154 if (WN_opcode(wn_tree) != OPC_IF)
00155 return FALSE;
00156 LWN_Simplify_Tree(WN_if_test(wn_tree));
00157 WN* wn_test = WN_if_test(wn_tree);
00158 OPERATOR opr = WN_operator(wn_test);
00159 if (!(opr == OPR_LT || opr == OPR_LE || opr == OPR_GT || opr == OPR_GE))
00160 return FALSE;
00161 if (WN_first(WN_then(wn_tree)) == NULL)
00162 return FALSE;
00163 if (WN_next(WN_first(WN_then(wn_tree))) != NULL)
00164 return FALSE;
00165 if (WN_first(WN_else(wn_tree)) == NULL)
00166 return FALSE;
00167 if (WN_next(WN_first(WN_else(wn_tree))) != NULL)
00168 return FALSE;
00169 LWN_Simplify_Tree(WN_first(WN_then(wn_tree)));
00170 LWN_Simplify_Tree(WN_first(WN_else(wn_tree)));
00171 WN* wn_st_then = WN_first(WN_then(wn_tree));
00172 WN* wn_st_else = WN_first(WN_else(wn_tree));
00173 if (!Matching_Stores(wn_st_then, wn_st_else))
00174 return FALSE;
00175 WN* wn_expr_then = Store_Expr(wn_st_then);
00176 WN* wn_expr_else = Store_Expr(wn_st_else);
00177 WN* wn_expr_left = WN_kid0(wn_test);
00178 WN* wn_expr_right = WN_kid1(wn_test);
00179 BOOL mm_max = FALSE;
00180 BOOL return_value = FALSE;
00181 if (Matching_Exprs(wn_expr_left, wn_expr_else)) {
00182 if (Matching_Exprs(wn_expr_right, wn_expr_then)) {
00183 mm_max = opr == OPR_LT || opr == OPR_LE;
00184 return_value = TRUE;
00185
00186 #if defined (TARG_SL) && defined (EMULATE_FLOAT_POINT)
00187
00188 if (MTYPE_is_float(WN_rtype(wn_expr_left)) || MTYPE_is_float(WN_rtype(wn_expr_right)))
00189 return FALSE;
00190 #endif
00191
00192 }
00193 } else if (Matching_Exprs(wn_expr_right, wn_expr_else)) {
00194 if (Matching_Exprs(wn_expr_left, wn_expr_then)) {
00195 mm_max = opr == OPR_GT || opr == OPR_GE;
00196 return_value = TRUE;
00197
00198 #if defined (TARG_SL) && defined (EMULATE_FLOAT_POINT)
00199
00200 if (MTYPE_is_float(WN_rtype(wn_expr_left)) || MTYPE_is_float(WN_rtype(wn_expr_right)))
00201 return FALSE;
00202 #endif
00203
00204 }
00205 }
00206 *if_mm_max = mm_max;
00207 return return_value;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217 static WN* IFMM_Convert(WN* wn_if,
00218 BOOL ifmm_max)
00219 {
00220 WN* wn_left = WN_kid0(WN_if_test(wn_if));
00221 WN* wn_right = WN_kid1(WN_if_test(wn_if));
00222 WN* wn_result = WN_first(WN_then(wn_if));
00223 WN* wn_result_expr = Store_Expr(wn_result);
00224 INT i;
00225 for (i = 0; i < WN_kid_count(wn_result_expr); i++)
00226 if (WN_kid(LWN_Get_Parent(wn_result_expr), i) == wn_result_expr)
00227 break;
00228 INT kid_count = i;
00229 TYPE_ID type_cmp = Max_Wtype(WN_rtype(wn_left), WN_rtype(wn_right));
00230 OPCODE op = OPCODE_make_op(ifmm_max ? OPR_MAX : OPR_MIN, type_cmp, MTYPE_V);
00231 WN* wn_cmp = LWN_CreateExp2(op, wn_left, wn_right);
00232 WN_kid0(WN_if_test(wn_if)) = NULL;
00233 WN_kid1(WN_if_test(wn_if)) = NULL;
00234 LWN_Set_Parent(wn_cmp, wn_result);
00235 WN_kid(wn_result, kid_count) = wn_cmp;
00236 LWN_Extract_From_Block(wn_result);
00237 LWN_Insert_Block_Before(LWN_Get_Parent(wn_if), wn_if, wn_result);
00238 LWN_Extract_From_Block(wn_if);
00239 LWN_Delete_Tree(wn_if);
00240 LWN_Delete_Tree(wn_result_expr);
00241 return wn_result;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251 static BOOL Is_Loop_Lower_Bound(WN* wn_use)
00252 {
00253 if (WN_operator(wn_use) != OPR_LDID)
00254 return FALSE;
00255 WN* wn_start = LWN_Get_Parent(wn_use);
00256 if (wn_start == NULL)
00257 return FALSE;
00258 WN* wn_loop = LWN_Get_Parent(wn_start);
00259 if (wn_loop == NULL)
00260 return FALSE;
00261 if (WN_opcode(wn_loop) != OPC_DO_LOOP)
00262 return FALSE;
00263 if (wn_start != WN_start(wn_loop))
00264 return FALSE;
00265 if (wn_use != WN_kid0(wn_start))
00266 return FALSE;
00267 return TRUE;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277 static BOOL Is_Loop_Upper_Bound(WN* wn_use)
00278 {
00279 if (WN_operator(wn_use) != OPR_LDID)
00280 return FALSE;
00281 WN* wn_end = LWN_Get_Parent(wn_use);
00282 if (wn_end == NULL)
00283 return FALSE;
00284 WN* wn_loop = LWN_Get_Parent(wn_end);
00285 if (wn_loop == NULL)
00286 return FALSE;
00287 if (WN_opcode(wn_loop) != OPC_DO_LOOP)
00288 return FALSE;
00289 if (wn_end != WN_end(wn_loop))
00290 return FALSE;
00291 if (wn_use != UBexp(WN_end(wn_loop)))
00292 return FALSE;
00293 return TRUE;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302 static void IFMM_Sink(WN* wn_max_store)
00303 {
00304 if (WN_operator(wn_max_store) != OPR_STID)
00305 return;
00306 USE_LIST *use_list = du->Du_Get_Use(wn_max_store);
00307 if (use_list == NULL)
00308 return;
00309 USE_LIST_ITER iter(use_list);
00310 const DU_NODE* node = NULL;
00311 const DU_NODE* nnode = NULL;
00312 for (node = iter.First(); !iter.Is_Empty(); node = nnode) {
00313 WN* wn_use = node->Wn();
00314 nnode = iter.Next();
00315 if (Is_Loop_Lower_Bound(wn_use)) {
00316 WN* wn_loop = LWN_Get_Parent(LWN_Get_Parent(wn_use));
00317 Forward_Substitute_Ldids(wn_use, du);
00318 DO_LOOP_INFO* dli = Get_Do_Loop_Info(wn_loop);
00319 if (Bound_Is_Too_Messy(dli->LB))
00320 Hoist_Bounds_One_Level(wn_loop);
00321 } else if (Is_Loop_Upper_Bound(wn_use)) {
00322 WN* wn_loop = LWN_Get_Parent(LWN_Get_Parent(wn_use));
00323 Forward_Substitute_Ldids(wn_use, du);
00324 DO_LOOP_INFO* dli = Get_Do_Loop_Info(wn_loop);
00325 if (Bound_Is_Too_Messy(dli->UB))
00326 Hoist_Bounds_One_Level(wn_loop);
00327 }
00328 }
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 static void If_MinMax_Traverse(WN* wn_tree)
00338 {
00339 BOOL ifmm_max = FALSE;
00340 if (IFMM_Convertible(wn_tree, &ifmm_max)) {
00341 WN* wn_max_store = IFMM_Convert(wn_tree, ifmm_max);
00342 IFMM_Sink(wn_max_store);
00343 return;
00344 }
00345
00346 if (WN_opcode(wn_tree) == OPC_BLOCK) {
00347 WN* wnn = NULL;
00348 for (WN* wn = WN_first(wn_tree); wn != NULL; wn = wnn) {
00349 wnn = WN_next(wn);
00350 If_MinMax_Traverse(wn);
00351 }
00352 } else {
00353 for (INT i = 0; i < WN_kid_count(wn_tree); i++)
00354 If_MinMax_Traverse(WN_kid(wn_tree, i));
00355 }
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 extern void If_MinMax(WN* func_nd)
00376 {
00377 if (!LNO_IfMinMax)
00378 return;
00379 if (LNO_Verbose) {
00380 fprintf(stdout, "Attempting to convert IFs to MAXs and MINs\n");
00381 fprintf(TFile, "Attempting to convert IFs to MAXs and MINs\n");
00382 }
00383 du = Du_Mgr;
00384 If_MinMax_Traverse(func_nd);
00385 if (LNO_Verbose) {
00386 fprintf(stdout, "Finished converting IFs to MAXs and MINs\n");
00387 fprintf(TFile, "Finished converting IFs to MAXs and MINs\n");
00388 }
00389 }