5 File name: ir/ana/pto_comp.c
6 Purpose: Main Implementation of PTO
9 Created: Sat Nov 13 19:35:27 CET 2004
11 Copyright: (c) 1999-2004 Universität Karlsruhe
12 Licence: This file is protected by the GPL - GNU GENERAL PUBLIC LICENSE.
20 pto_comp: Main Implementation of PTO
23 # include <string.h> /* for memset */
25 # include "pto_comp.h"
26 # include "pto_util.h"
27 # include "pto_name.h"
31 # include "irnode_t.h"
34 # include "irmemwalk.h"
36 # include "pto_debug.h"
37 # include "pto_init.h"
43 /* Local Data Types: */
44 typedef struct pto_env_str {
50 /* Local Variables: */
55 /* Local Prototypes: */
56 static pto_t *get_pto (ir_node*, pto_env_t*);
57 static void pto_call (ir_graph*, ir_node*, pto_env_t*);
58 static void pto_raise (ir_node*, pto_env_t*);
59 static void pto_load (ir_node*, pto_env_t*);
60 static void pto_store (ir_node*, pto_env_t*);
61 static void pto_method (ir_node*, pto_env_t*);
62 static void pto_end_block (ir_node*, pto_env_t*);
64 /* ===================================================
66 =================================================== */
67 /* Transfer the actual arguments to the formal arguments */
68 static void set_graph_args (ir_graph *graph, ir_node *call, pto_env_t *env)
70 assert (iro_Call == get_irn_opcode (call));
72 type *meth = get_entity_type (get_irg_entity (graph));
73 ir_node **args = find_irg_args (graph);
74 int n_args = get_Call_n_params (call);
77 for (i = 0; i < n_args; i ++) {
78 if (NULL != args [i]) {
79 if (mode_P == get_type_mode (get_method_param_type (meth, i))) {
80 ir_node *call_arg = get_Call_param (call, i);
81 pto_t *pto = get_pto (call_arg, env);
83 set_node_pto (args [i], pto);
85 DBGPRINT (1, (stdout, "%s: arg [%i]: %s[%li] -> %s[%li] (%i)\n",
88 OPNAME (call_arg), OPNUM (call_arg),
89 OPNAME (args [i]), OPNUM (args [i]),
96 /* Transfer the graph's result to the call */
97 static int set_graph_result (ir_graph *graph, ir_node *call)
99 type *tp = get_entity_type (get_irg_entity (graph));
102 if (0 == get_method_n_ress (tp)) {
106 tp = get_method_res_type (tp, 0);
108 if (mode_P != get_type_mode (tp)) {
109 set_node_pto (call, NULL);
114 ir_node *end_block = get_irg_end_block (graph);
115 pto_t *ret_pto = get_node_pto (end_block);
117 pto_t *call_pto = get_node_pto (call);
121 change |= qset_insert_all (call_pto->values, ret_pto->values);
126 /* Propagation of PTO values */
127 static pto_t *get_pto_proj (ir_node *proj, pto_env_t *env)
129 ir_node *proj_in = get_Proj_pred (proj);
130 const long proj_proj = get_Proj_proj (proj);
131 const opcode in_op = get_irn_opcode (proj_in);
132 pto_t *in_pto = NULL;
133 pto_t *proj_pto = NULL; /* get_node_pto (proj); */
135 ir_node *proj_in_in = NULL;
138 case (iro_Start): /* ProjT (Start) */
139 assert (0 && "pto from ProjT(Start) requested");
142 case (iro_Proj): /* ProjT (Start), ProjT (Call) */
143 proj_in_in = get_Proj_pred (proj_in);
144 const opcode in_in_op = get_irn_opcode (proj_in_in);
145 const long proj_in_proj = get_Proj_proj (proj_in);
147 assert ((pn_Start_T_args == proj_in_proj) ||
148 (pn_Call_T_result == proj_in_proj));
151 case (iro_Start): /* ProjArg (ProjT (Start)) */
152 /* then the pto value must have been set to the node */
153 proj_pto = get_node_pto (proj);
157 case (iro_Call): /* ProjV (ProjT (Call)) */
158 if (NULL != proj_pto) {
161 in_pto = get_pto (proj_in, env);
162 set_node_pto (proj, in_pto);
168 default: assert (0 && "Proj(Proj(?))");
170 /* done with case (in_op == iro_Proj) */
172 case (iro_Load): /* ProjV (Load) */
173 assert (pn_Load_res == proj_proj);
175 case (iro_Call): /* ProjT (Call) */
177 case (iro_Alloc): /* ProjV (Alloc) */
178 if (NULL != proj_pto) {
181 in_pto = get_pto (proj_in, env);
184 set_node_pto (proj, in_pto);
188 fprintf (stderr, "%s: not handled: proj (%s[%li])\n",
190 get_op_name (get_irn_op (proj_in)),
191 get_irn_node_nr (proj_in));
192 assert (0 && "Proj(?)");
197 static pto_t *get_pto_phi (ir_node *phi, pto_env_t *env)
199 assert (mode_P == get_irn_mode (phi));
201 pto_t *pto = get_node_pto (phi);
204 assert (pto); /* must be initialised */
206 int n_ins = get_irn_arity (phi);
209 for (i = 0; i < n_ins; i ++) {
210 ir_node *in = get_irn_n (phi, i);
211 pto_t *in_pto = get_pto (in, env);
215 change |= qset_insert_all (pto->values, in_pto->values);
218 env->change |= change;
223 static pto_t *get_pto_sel (ir_node *sel, pto_env_t *env)
225 pto_t *pto = NULL; /* get_node_pto (sel); */
228 ir_node *in = get_Sel_ptr (sel);
230 pto = get_pto (in, env);
231 set_node_pto (sel, pto);
237 static pto_t *get_pto_ret (ir_node *ret, pto_env_t *env)
239 pto_t *pto = NULL; /* get_node_pto (ret); */
242 ir_node *in = get_Return_res (ret, 0);
244 pto = get_pto (in, env);
245 set_node_pto (ret, pto);
254 /* Dispatch to propagate PTO values */
255 static pto_t *get_pto (ir_node *node, pto_env_t *env)
257 const opcode op = get_irn_opcode (node);
259 DBGPRINT (2, (stdout, "%s (%s[%li])\n", __FUNCTION__,
260 OPNAME (node), OPNUM (node)));
263 case (iro_Cast): return (get_pto (get_Cast_op (node), env));
264 case (iro_Proj): return (get_pto_proj (node, env));
265 case (iro_Phi): return (get_pto_phi (node, env));
266 case (iro_Sel): return (get_pto_sel (node, env));
267 case (iro_Alloc): return (get_alloc_pto (node));
268 case (iro_Return): return (get_pto_ret (node, env));
270 case (iro_Call): /* FALLTHROUGH */
271 case (iro_Load): /* FALLTHROUGH */
272 case (iro_Const): /* FALLTHROUGH */
273 case (iro_SymConst): return (get_node_pto (node));
276 /* stopgap measure */
277 fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
279 get_irn_node_nr (node),
280 get_op_name (get_irn_op (node)));
281 assert (0 && "something not handled");
287 /* Actions for the nodes: */
288 static void pto_load (ir_node *load, pto_env_t *pto_env)
291 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = %p\n", __FUNCTION__,
292 OPNAME (load), OPNUM (load), (void*) get_node_pto (load)));
294 ir_node *ptr = get_Load_ptr (load);
296 if (is_dummy_load_ptr (ptr)) {
300 entity *ent = get_ptr_ent (ptr);
301 pto_t *ptr_pto = get_pto (ptr, pto_env);
305 DBGPRINT (1, (stdout, "%s (%s[%li]): ptr = %p\n", __FUNCTION__,
306 OPNAME (ptr), OPNUM (ptr), (void*) ptr_pto));
308 pto_env->change |= mod_load (load, ent, ptr_pto);
311 static void pto_store (ir_node *store, pto_env_t *pto_env)
314 DBGPRINT (2, (stdout, "%s (%s[%li]) (no pto)\n", __FUNCTION__,
315 OPNAME (store), OPNUM (store)));
317 ir_node *ptr = get_Store_ptr (store);
318 ir_node *val = get_Store_value (store);
320 if (mode_P != get_irn_mode (val)) {
324 entity *ent = get_ptr_ent (ptr);
326 pto_t *ptr_pto = get_pto (ptr, pto_env);
327 pto_t *val_pto = get_pto (val, pto_env);
332 DBGPRINT (2, (stdout, "%s (%s[%li]): ptr_pto = %p\n", __FUNCTION__,
333 OPNAME (ptr), OPNUM (ptr), (void*) ptr_pto));
334 DBGPRINT (2, (stdout, "%s (%s[%li]): val_pto = %p\n", __FUNCTION__,
335 OPNAME (val), OPNUM (val), (void*) val_pto));
337 pto_env->change |= mod_store (store, ent, ptr_pto, val_pto);
340 static void pto_method (ir_node *call, pto_env_t *pto_env)
342 DBGPRINT (2, (stdout, "%s:%i (%s[%li]): pto = %p\n",
343 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call),
344 (void*) get_node_pto (call)));
346 callEd_info_t *callEd_info = ecg_get_callEd_info (call);
348 if (NULL == callEd_info) {
349 DBGPRINT (2, (stdout, "%s:%i (%s[%li]), no graph\n",
350 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call)));
354 while (NULL != callEd_info) {
355 DBGPRINT (2, (stdout, "%s:%i (%s[%li]), graph %i\n",
356 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call), i ++));
358 pto_call (callEd_info->callEd, call, pto_env);
360 callEd_info = callEd_info->prev;
364 /* Perform the appropriate action on the given node */
365 static void pto_node_node (ir_node *node, pto_env_t *pto_env)
367 DBGPRINT (1, (stdout, "%s (%s[%li])\n", __FUNCTION__,
368 OPNAME (node), OPNUM (node)));
370 const opcode op = get_irn_opcode (node);
373 case (iro_Start): /* nothing */ break;
375 pto_load (node, pto_env);
379 pto_store (node, pto_env);
383 pto_method (node, pto_env);
387 pto_raise (node, pto_env);
399 pto_end_block (node, pto_env);
404 assert (mode_M == get_irn_mode (node));
408 /* uninteresting stuff: */
412 case (iro_DivMod): /* nothing to do */ break;
415 /* stopgap measure */
416 fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
418 get_irn_node_nr (node),
419 get_op_name (get_irn_op (node)));
420 assert (0 && "something not handled");
425 /* Callback function to execute in pre-order */
426 static void pto_node_pre (ir_node *node, void *env)
431 /* Callback function to execute in post-order */
432 static void pto_node_post (ir_node *node, void *env)
434 pto_env_t *pto_env = (pto_env_t*) env;
436 pto_node_node (node, pto_env);
439 /* Perform a single pass over the given graph */
440 static void pto_graph_pass (ir_graph *graph, void *pto_env)
442 entity *ent = get_irg_entity (graph);
443 const char *ent_name = (char*) get_entity_name (ent);
444 const char *own_name = (char*) get_type_name (get_entity_owner (ent));
445 /* HERE3 ("start", own_name, ent_name); */
447 irg_walk_mem (graph, pto_node_pre, pto_node_post, pto_env);
449 /* HERE3 ("end ", own_name, ent_name); */
452 /* Continue PTO for one of the graphs called at a Call */
453 static void pto_call (ir_graph *graph, ir_node *call, pto_env_t *pto_env)
458 DBGPRINT (1, (stdout, "%s:%i (%s[%li])\n",
459 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call)));
461 /* only for debugging stuff: */
462 entity *ent = get_irg_entity (graph);
463 const char *ent_name = (char*) get_entity_name (ent);
464 const char *own_name = (char*) get_type_name (get_entity_owner (ent));
466 if (! get_irg_is_mem_visited (graph)) {
467 graph_info_t *ginfo = ecg_get_info (graph);
470 int ctx_idx = find_ctx_idx (call, ginfo, get_curr_ctx ());
471 ctx_info_t *call_ctx = get_ctx (ginfo, ctx_idx);
472 ctx_info_t *old_ctx = set_curr_ctx (call_ctx);
473 DBGPRINT (3, (stdout, "%s>CTX: ", -- spaces));
474 DBGEXE (3, ecg_print_ctx (call_ctx, stdout));
476 /* Initialise Alloc Names and Node values */
477 pto_reset_graph_pto (graph, ctx_idx);
479 /* Compute Arguments */
480 set_graph_args (graph, call, pto_env);
482 /* Visit/Iterate Graph */
483 pto_graph (graph, ctx_idx);
486 set_curr_ctx (old_ctx);
488 change |= set_graph_result (graph, call);
490 DBGPRINT (3, (stdout, "%s<CTX: ", spaces ++));
491 DBGEXE (3, ecg_print_ctx (call_ctx, stdout));
493 /* Don't need to reset alloc names unless we handle recursion here */
496 /* Get Return Value from Graph */
498 DBGPRINT (2, (stdout, "%s: recursion into \"%s.%s\"\n",
499 __FUNCTION__, own_name, ent_name));
502 /* Todo: Set 'Unknown' Value as Return Value when the graph is not
505 pto_env->change |= change;
508 static void pto_raise (ir_node *raise, pto_env_t *pto_env)
511 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = %p\n", __FUNCTION__,
512 OPNAME (raise), OPNUM (raise), (void*) get_node_pto (raise)));
515 static void pto_end_block (ir_node *end_block, pto_env_t *pto_env)
517 /* perform end block */
518 type *tp = get_entity_type (get_irg_entity (get_irn_irg (end_block)));
520 if (0 == get_method_n_ress (tp)) {
524 tp = get_method_res_type (tp, 0);
526 if (mode_P != get_type_mode (tp)) {
530 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = %p\n", __FUNCTION__,
531 OPNAME (end_block), OPNUM (end_block),
532 (void*) get_node_pto (end_block)));
534 pto_t *end_pto = get_node_pto (end_block);
538 int n_ins = get_irn_arity (end_block);
540 for (i = 0; i < n_ins; i ++) {
541 ir_node *in = get_irn_n (end_block, i);
543 if (iro_Return == get_irn_opcode (in)) {
544 pto_t *in_pto = get_pto (in, pto_env);
546 pto_env->change |= qset_insert_all (end_pto->values, in_pto->values);
551 /* ===================================================
552 Exported Implementation:
553 =================================================== */
554 /* Main loop: Initialise and iterate over the given graph */
555 void pto_graph (ir_graph *graph, int ctx_idx)
557 /* Also exported, since we need it in 'pto.c' */
558 pto_env_t *pto_env = xmalloc (sizeof (pto_env_t));
559 pto_env->ctx_idx = ctx_idx;
560 pto_env->change = TRUE;
562 /* HERE ("start"); */
564 DBGPRINT (2, (stdout, "%s: start for ctx %i\n",
568 /* todo (here): iterate, obey 'changed' attribute */
570 while (0 != pto_env->change) {
572 pto_env->change = FALSE;
573 pto_graph_pass (graph, pto_env);
576 DBGPRINT (1, (stdout, "%s: %i runs on \"%s.%s\"\n",
579 get_type_name (get_entity_owner (get_irg_entity (graph))),
580 get_entity_name (get_irg_entity (graph))));
583 memset (pto_env, 0x00, sizeof (pto_env_t));
588 /* Set the PTO value for the given non-alloc node */
589 void set_node_pto (ir_node *node, pto_t *pto)
591 assert (iro_Alloc != get_irn_opcode (node));
593 set_irn_link (node, (void*) pto);
596 /*Get the PTO value for the given non-alloc node */
597 pto_t *get_node_pto (ir_node *node)
599 assert (iro_Alloc != get_irn_opcode (node));
601 return ((pto_t*) get_irn_link (node));
604 /* Set the PTO value for the given alloc node */
605 void set_alloc_pto (ir_node *alloc, alloc_pto_t *alloc_pto)
607 assert (iro_Alloc == get_irn_opcode (alloc));
611 set_irn_link (alloc, (void*) alloc_pto);
614 /*Get the current PTO value for the given alloc node */
615 pto_t *get_alloc_pto (ir_node *alloc)
617 alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (alloc);
619 assert (iro_Alloc == get_irn_opcode (alloc));
621 assert (alloc_pto -> curr_pto);
623 return (alloc_pto -> curr_pto);
629 Revision 1.7 2004/12/06 12:55:06 liekweg
632 Revision 1.6 2004/12/02 16:17:51 beck
633 fixed config.h include
635 Revision 1.5 2004/11/30 14:47:54 liekweg
636 fix initialisation; do correct iteration
638 Revision 1.4 2004/11/26 15:59:40 liekweg
639 verify pto_{load,store}
641 Revision 1.3 2004/11/24 14:53:55 liekweg
644 Revision 1.2 2004/11/20 21:21:56 liekweg
645 Finalise initialisation
647 Revision 1.1 2004/11/18 16:37:34 liekweg