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 #define __STDC_LIMIT_MACROS
00054 #ifdef __MINGW32__
00055 #include <WINDOWS.h>
00056 #endif
00057 #include <stdint.h>
00058 #include <map>
00059 #include <cxx_memory.h>
00060 #include "inline_utils.h"
00061
00062 IP_FILE_HDR&
00063 Setup_Inliner_File_Header (char *input_name, void *mmap_addr)
00064 {
00065 UINT index;
00066
00067 IP_FILE_HDR& file_header = IP_File_header.New_entry (index);
00068 new (&file_header) IP_FILE_HDR (input_name, mmap_addr, 0);
00069
00070 return file_header;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 typedef vector<ST*, mempool_allocator<ST*> > COMMON_BLOCK;
00085
00086
00087 struct compare_block
00088 {
00089 bool operator() (const ST* st1, const ST* st2) const {
00090 if (st1 == st2)
00091 return false;
00092
00093 if (ST_name_idx (st1) != ST_name_idx (st2))
00094 return ST_name_idx (st1) < ST_name_idx (st2);
00095 if (ST_sclass (st1) != ST_sclass (st2))
00096 return ST_sclass (st1) == SCLASS_DGLOBAL;
00097 UINT64 size1 = TY_size (ST_type (st1));
00098 UINT64 size2 = TY_size (ST_type (st2));
00099 if (size1 != size2)
00100 return size1 > size2;
00101 return st1 < st2;
00102 }
00103 };
00104
00105 typedef std::map<const ST*, COMMON_BLOCK*, compare_block,
00106 mempool_allocator <std::pair<const ST* const, COMMON_BLOCK*> > >
00107 COMMON_MAP;
00108
00109
00110
00111 struct collect_commons
00112 {
00113 COMMON_MAP& common_map;
00114 MEM_POOL* pool;
00115
00116 collect_commons (COMMON_MAP& cm, MEM_POOL* p) :
00117 common_map (cm), pool (p) {}
00118
00119 COMMON_BLOCK* get_common_block (ST* st) const {
00120 COMMON_MAP::iterator iter = common_map.find (st);
00121 if (iter == common_map.end ()) {
00122 COMMON_BLOCK* p = CXX_NEW (COMMON_BLOCK (pool), pool);
00123 common_map[st] = p;
00124 p->push_back (st);
00125 return p;
00126 }
00127 return (*iter).second;
00128 }
00129
00130 ST* ST_raw_base_idx (ST* st) const {
00131 if (st->base_idx == ST_st_idx (st))
00132 return st;
00133 ST* base_st = &St_Table[st->base_idx];
00134 while (ST_st_idx (base_st) != base_st->base_idx)
00135 base_st = &St_Table[base_st->base_idx];
00136 return base_st;
00137 }
00138
00139 void operator() (UINT32, ST* st) const {
00140 if (ST_sclass (st) == SCLASS_DGLOBAL ||
00141 ST_sclass (st) == SCLASS_COMMON) {
00142
00143 if (ST_export (st) == EXPORT_LOCAL ||
00144 ST_export (st) == EXPORT_LOCAL_INTERNAL) {
00145
00146 if (ST_base_idx (st) == ST_st_idx (st))
00147 return;
00148 }
00149
00150 ST* base_st = ST_raw_base_idx (st);
00151 COMMON_BLOCK* p = get_common_block (base_st);
00152 p->push_back (st);
00153 }
00154 }
00155 };
00156
00157
00158
00159 static void
00160 Fix_Base_ST (ST* base_st, UINT64 size)
00161 {
00162 TY_IDX ty_idx = Copy_TY (ST_type (base_st));
00163 Set_TY_size (Ty_Table[ty_idx], size);
00164
00165 TY_IDX unique_idx = TY_is_unique (ty_idx);
00166 if (unique_idx != ty_idx) {
00167
00168
00169 Ty_tab.Delete_last ();
00170 Set_ST_type (base_st, unique_idx);
00171 } else
00172 Set_ST_type (base_st, ty_idx);
00173
00174 }
00175
00176
00177
00178
00179 static void
00180 Mark_Alaised (ST* base_st, const COMMON_BLOCK* block)
00181 {
00182 ST* block_st = block->front ();
00183
00184
00185
00186 if (ST_sclass (base_st) == SCLASS_DGLOBAL &&
00187 TY_size (ST_type (base_st)) < TY_size (ST_type (block_st))) {
00188
00189
00190
00191 Fix_Base_ST (base_st, TY_size (ST_type (block_st)));
00192 }
00193
00194 Set_ST_base_idx (block_st, ST_st_idx (base_st));
00195
00196 if (ST_sclass (base_st) == ST_sclass (block_st))
00197 return;
00198
00199 for (COMMON_BLOCK::const_iterator iter (block->begin ());
00200 iter != block->end (); ++iter) {
00201
00202 Set_ST_sclass (*iter, ST_sclass (base_st));
00203
00204 if (ST_is_initialized (base_st)) {
00205 Set_ST_is_initialized (*iter);
00206 if (ST_init_value_zero (base_st))
00207 Set_ST_init_value_zero (*iter);
00208 }
00209 }
00210
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 void
00223 Fix_Aliased_Commons ()
00224 {
00225 CXX_MEM_POOL pool ("aliased_commons", FALSE);
00226
00227 compare_block tmp;
00228 COMMON_MAP common_map (tmp, pool());
00229
00230 For_all (St_Table, GLOBAL_SYMTAB, collect_commons (common_map, pool()));
00231
00232 COMMON_MAP::const_iterator iter (common_map.begin ());
00233 while (iter != common_map.end ()) {
00234 ST* base_st = iter->second->front ();
00235 ++iter;
00236 while (iter != common_map.end () &&
00237 ST_name_idx (base_st) == ST_name_idx (iter->second->front ())) {
00238
00239 Mark_Alaised (base_st, iter->second);
00240 ++iter;
00241 }
00242 }
00243 }
00244
00245 #ifdef _LIGHTWEIGHT_INLINER
00246
00247 #include <unistd.h>
00248 #include <fcntl.h>
00249 #ifndef __MINGW32__
00250 #include <sys/mman.h>
00251 #endif
00252 #if defined(BUILD_OS_DARWIN)
00253 #include <darwin_elf.h>
00254 #else
00255 #include <elf.h>
00256 #endif
00257 #include <sys/elf_whirl.h>
00258 #include "defs.h"
00259
00260 #include "pu_info.h"
00261 #include "ir_bwrite.h"
00262 #include "ir_bcom.h"
00263 #include "ir_bread.h"
00264
00265 #include "wn_tree_util.h"
00266 #include "ipa_cg.h"
00267
00268
00269
00270
00271 BOOL
00272 Need_To_Inline_Callee (WN *w)
00273 {
00274 WN* w2;
00275
00276
00277 for (WN_TREE_ITER<PRE_ORDER, WN*> iter (w); iter.Wn () != NULL; ++iter) {
00278
00279 w2 = iter.Wn ();
00280
00281 switch (WN_operator(w2)) {
00282
00283 case OPR_CALL:
00284
00285
00286 if (PU_is_inline_function(Pu_Table[ST_pu(WN_st(w2))])) {
00287 #ifdef DEBUG
00288 printf ("Need to inline %s into %s\n", ST_name(WN_st(w2)), ST_name(WN_st(w));
00289
00290 #endif // DEBUG
00291 return TRUE;
00292 }
00293 break;
00294
00295 default:
00296 break;
00297 }
00298 }
00299
00300 return FALSE;
00301
00302 }
00303
00304
00305
00306 void
00307 Copy_Input_Info_To_Output(char *input_name, char *output_name)
00308 {
00309
00310 off_t mapped_size;
00311 #ifdef __MINGW32__
00312 HANDLE handle;
00313 void *i_handle = WN_open_input (input_name, &mapped_size, 0, &handle);
00314 #else
00315 void *i_handle = WN_open_input (input_name, &mapped_size);
00316 #endif // __MINGW32__
00317 void *o_handle = WN_open_output(output_name);
00318 (void) ir_b_copy_file (i_handle, mapped_size, o_handle);
00319 WN_close_file(o_handle);
00320 }
00321
00322
00323 #endif // _LIGHTWEIGHT_INLINER