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 #define __STDC_LIMIT_MACROS
00041 #include <stdint.h>
00042 #include <sys/types.h>
00043 #include <stdio.h>
00044 #include <assert.h>
00045 #include <math.h>
00046 #include "lnopt_main.h"
00047 #include "access_main.h"
00048 #include "access_vector.h"
00049 #include "dep.h"
00050 #include "cxx_memory.h"
00051 #include "soe.h"
00052 #include "dnf.h"
00053 #include "cxx_queue.h"
00054 #include "lwn_util.h"
00055 #include "shackle_ifs.h"
00056 #include "opt_du.h"
00057 #include "lego_util.h"
00058 #include "fiz_fuse.h"
00059 #include "errors.h"
00060 #include "whirl2src.h"
00061 #include "scalar_expand.h"
00062 #include "shackle.h"
00063
00064 static MEM_POOL *shackle_mem_pool;
00065 extern QUEUE<WN *> *Extract_Stmts_With_Chain_Id (QUEUE<WN *> *,
00066 INT32);
00067 extern BOOL Ref_Is_Significant (QUEUE<ACCESS_ARRAY *> *,
00068 ACCESS_ARRAY *);
00069 extern SHACKLE_INFO *Shackle_Info_For_Symbol (QUEUE<SHACKLE_INFO*> *,
00070 const ST*);
00071 extern ST *Identical_Array_Refbase (WN *, WN *);
00072 extern QUEUE<WN *> *gather_stmts_in_func(WN *);
00073 extern WN_MAP shackle_shackle_map;
00074 extern TY_IDX Shackle_Is_Array_Type (TY_IDX);
00075 extern INT64 Shackle_Base_Type_Size (TY_IDX array_type);
00076
00077 static INT32
00078 Shackle_Mem_Wn_Depth(WN *main_snl, INT32 current)
00079 {
00080 if (OPC_BLOCK != WN_opcode (main_snl) &&
00081 0 == WN_kid_count (main_snl))
00082 return current;
00083 else if (OPC_DO_LOOP == WN_opcode (main_snl))
00084 return Shackle_Mem_Wn_Depth (WN_do_body (main_snl),
00085 current+1);
00086 else {
00087 INT32 depth = 0, ndepth;
00088 FOR_CHILDREN (main_snl, child, ignCount) {
00089 ndepth = Shackle_Mem_Wn_Depth (child, current);
00090 if (ndepth > depth)
00091 depth = ndepth;
00092 }
00093 END_CHILDREN;
00094 return depth;
00095 }
00096 }
00097
00098 static void
00099 Gather_References_At_Deepest_Depth(WN *main_snl,
00100 INT32 current_depth,
00101 INT32 max_depth,
00102 QUEUE<WN *> *ref_queue)
00103 {
00104 if ((current_depth == max_depth) &&
00105 OPR_ARRAY == WN_operator (main_snl)) {
00106 ACCESS_ARRAY *ar_main = (ACCESS_ARRAY *)
00107 WN_MAP_Get (LNO_Info_Map, main_snl);
00108 QUEUE_ITER<WN *> iter(ref_queue);
00109 WN *array_step;
00110 QUEUE<ACCESS_ARRAY*> *test_q =
00111 CXX_NEW (QUEUE<ACCESS_ARRAY *> (shackle_mem_pool),
00112 shackle_mem_pool);
00113 BOOL can_add_ref = TRUE;
00114 while (iter.Step (&array_step)) {
00115 FmtAssert (OPR_ARRAY == WN_operator (array_step),
00116 ("Ref queue contains Non arrays!"));
00117 ACCESS_ARRAY *test_ar = (ACCESS_ARRAY *)
00118 WN_MAP_Get (LNO_Info_Map, array_step);
00119 test_q->Add_Tail_Q (test_ar);
00120
00121
00122 if (!Ref_Is_Significant (test_q, ar_main)) {
00123 can_add_ref = FALSE;
00124 break;
00125 }
00126 (void *) test_q->Get_Tail_Q();
00127 FmtAssert (test_q->Queue_Isempty(),
00128 ("Insertion followed by deletion - nonempty!"));
00129 }
00130 if (can_add_ref)
00131 ref_queue->Add_Tail_Q (main_snl);
00132 return;
00133 }
00134 if (OPC_DO_LOOP == WN_opcode (main_snl))
00135 Gather_References_At_Deepest_Depth (WN_do_body (main_snl),
00136 current_depth+1,
00137 max_depth,
00138 ref_queue);
00139 else {
00140 FOR_CHILDREN (main_snl, child, ignCount) {
00141 Gather_References_At_Deepest_Depth (child, current_depth,
00142 max_depth, ref_queue);
00143 }
00144 END_CHILDREN;
00145 }
00146 }
00147
00148 void
00149 Shackle_Mem_Initialize(MEM_POOL *pool)
00150 {
00151 shackle_mem_pool = pool;
00152 }
00153
00154 static QUEUE<WN *> *
00155 Gather_Deepest_References(WN *main_snl)
00156 {
00157 INT32 depth = Shackle_Mem_Wn_Depth (main_snl, 0);
00158 QUEUE<WN *> *refq =
00159 CXX_NEW (QUEUE<WN *> (shackle_mem_pool), shackle_mem_pool);
00160 Gather_References_At_Deepest_Depth (main_snl, 0,
00161 depth, refq);
00162 return refq;
00163 }
00164
00165 static BOOL
00166 Volume_Is_Symbolic(SHACKLE_INFO *sh)
00167 {
00168 INT32 i;
00169
00170 for (i = 0; i < sh->Ndim(); i++)
00171 if (!sh->Is_Const_Lower (i) || !sh->Is_Const_Upper (i))
00172 return TRUE;
00173 return FALSE;
00174 }
00175
00176 static mINT64
00177 Integral_Volume(SHACKLE_INFO *sh)
00178 {
00179 mINT64 vol = 1;
00180 INT32 i;
00181
00182 for (i = 0; i < sh->Ndim(); i++) {
00183 FmtAssert(sh->Is_Const_Lower (i) || sh->Is_Const_Upper (i),
00184 ("Must have const lower and upper bounds"));
00185 vol = vol * (sh->Const_Upper(i) - sh->Const_Lower(i));
00186 }
00187 return vol;
00188 }
00189
00190 static BOOL
00191 Cache_Is_Normal ()
00192 {
00193 INT64 current = 0;
00194 INT32 i;
00195
00196 for (i = Mhd.First(); i != -1; i = Mhd.Next(i)) {
00197 MHD_LEVEL *mhdp = &Mhd.L[i];
00198 if (mhdp->Valid()) {
00199 if (mhdp->Effective_Size < current)
00200 return FALSE;
00201 else
00202 current = mhdp->Effective_Size;
00203 }
00204 }
00205 FmtAssert (0 != current,
00206 ("Must have had some non-zero effective sized cache"));
00207 return TRUE;
00208 }
00209
00210 static INT32
00211 Last_Level_Of_Cache_Smaller(SHACKLE_INFO *sh)
00212 {
00213 BOOL is_symbolic = Volume_Is_Symbolic (sh);
00214 if (is_symbolic)
00215 return -1;
00216 BOOL cache_is_normal = Cache_Is_Normal();
00217 if (!cache_is_normal)
00218 return -1;
00219 INT64 volume = Integral_Volume (sh);
00220 INT32 i, prev = -1;
00221 MHD_LEVEL *mhdp;
00222
00223 for (i = Mhd.First(); i != -1; i = Mhd.Next(i)) {
00224 mhdp = &Mhd.L[i];
00225 if (mhdp->Valid()) {
00226 if (mhdp->Effective_Size > volume)
00227 return prev;
00228 else
00229 prev = i;
00230 }
00231 }
00232 FmtAssert (-1 == prev || mhdp->Effective_Size <= volume,
00233 ("Must be empty or too small outermost cache"));
00234 return prev;
00235 }
00236
00237 static INT64
00238 Nth_Integral_Root (INT64 val, INT64 root)
00239 {
00240 FmtAssert (root > 0,
00241 ("Cannot take a non-positive root of an integer"));
00242 FmtAssert (val >= 0,
00243 ("Cannot take a root of a non-positive integer"));
00244 double dval = (double) val;
00245 double dlog = log (dval);
00246 double droot = exp (dlog / ((double) root));
00247 FmtAssert (droot >= 0,
00248 ("Desired root cannot be negative"));
00249 return ((INT64) (0.5 + droot));
00250 }
00251
00252 static void
00253 Set_Shackle_Size_Info (SHACKLE_INFO *sh,
00254 INT64 allowed_size)
00255 {
00256
00257
00258
00259 BOOL unfortunate_case = FALSE;
00260 INT32 i;
00261
00262 for (i = 0; i < sh->Ndim(); i++)
00263 if (!sh->Is_Dim_Shackled (i) &&
00264 (!sh->Is_Const_Lower(i) || !sh->Is_Const_Upper (i))) {
00265 unfortunate_case = TRUE;
00266 break;
00267 }
00268 if (!unfortunate_case) {
00269 TY_IDX btype = Shackle_Is_Array_Type (ST_type (sh->Symbol()));
00270 INT64 vol = Shackle_Base_Type_Size(btype);
00271 FmtAssert (0 != vol,
00272 ("Couldn't have been shackling a non-array!"));
00273 for (i = 0; i < sh->Ndim(); i++)
00274 if (!sh->Is_Dim_Shackled (i)) {
00275 FmtAssert (sh->Is_Const_Lower(i) && sh->Is_Const_Upper (i),
00276 ("Not an unfortunate case!"));
00277 vol = vol * (sh->Const_Upper(i) - sh->Const_Lower(i));
00278 }
00279 INT64 common_size = Nth_Integral_Root (allowed_size/vol,
00280 sh->Ndim_Shackled());
00281 if (common_size > SHACKLED_DIM_MIN_SIZE)
00282 for (i = 0; i < sh->Ndim(); i++) {
00283 if (sh->Is_Dim_Shackled (i))
00284 sh->Set_Shackle_Dim_Size (i, (INT32) common_size);
00285 }
00286 else if (sh->Ndim_Shackled() <= MAX_SHACKLE_DIM_OF_MIN_SIZE)
00287 for (i = 0; i < sh->Ndim(); i++) {
00288 if (sh->Is_Dim_Shackled (i))
00289 sh->Set_Shackle_Dim_Size (i, SHACKLED_DIM_MIN_SIZE);
00290 }
00291 else
00292 for (i = 0; i < sh->Ndim(); i++) {
00293 sh->Set_Dim_Shackled (i, FALSE);
00294 sh->Set_Shackle_Dim_Size (i, 0);
00295 }
00296 return;
00297 }
00298 else {
00299 for (i = 0; i < sh->Ndim(); i++) {
00300 if (sh->Is_Dim_Shackled (i))
00301 sh->Set_Shackle_Dim_Size (i, (INT32)
00302 SHACKLED_DIM_MIN_SIZE);
00303 }
00304 return;
00305 }
00306 }
00307
00308 static void
00309 Set_Shackle_Size_Info (SHACKLE_INFO *sh,
00310 QUEUE<WN *> *refq)
00311 {
00312 if (0 == sh->Ndim_Shackled())
00313 return;
00314 INT32 level_of_cache = Last_Level_Of_Cache_Smaller (sh);
00315 if (-1 == level_of_cache) {
00316 for (INT32 i = 0; i < sh->Ndim(); i++) {
00317 sh->Set_Dim_Shackled (i, FALSE);
00318 sh->Set_Shackle_Dim_Size (i, (INT32) 0);
00319 }
00320 return;
00321 }
00322 INT64 cache_size = Mhd.L[level_of_cache].Effective_Size;
00323 INT64 allowed_size = cache_size / refq->Queue_Length();
00324 Set_Shackle_Size_Info (sh, allowed_size);
00325 }
00326
00327 BOOL
00328 Appropriate_Shackle_Size_Set(WN *main_snl,
00329 QUEUE<SHACKLE_INFO *> *shq)
00330 {
00331 QUEUE<WN *> *refq = Gather_Deepest_References (main_snl);
00332 INT32 count = refq->Queue_Length();
00333 QUEUE<WN *> *stmt_list = gather_stmts_in_func (main_snl);
00334 QUEUE<WN *> *unchained =
00335 Extract_Stmts_With_Chain_Id (stmt_list, 0);
00336 if (unchained->Queue_Isempty())
00337 return TRUE;
00338 WN *stmt = unchained->Queue_First()->Qnode_Item();
00339 QUEUE<WN *> *shackle = (QUEUE<WN *> *)
00340 WN_MAP_Get (shackle_shackle_map, stmt);
00341 QUEUE_ITER<WN *> iter(shackle);
00342 WN *shackle_ref;
00343
00344 while (iter.Step (&shackle_ref)) {
00345 const ST *st = Identical_Array_Refbase (shackle_ref, shackle_ref);
00346 SHACKLE_INFO *sh = Shackle_Info_For_Symbol (shq, st);
00347 if (NULL == sh)
00348 return FALSE;
00349 Set_Shackle_Size_Info (sh, refq);
00350 }
00351 return TRUE;
00352 }