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 {
45 struct pto_env_str *enc_env;
52 /* Local Variables: */
57 /* Local Prototypes: */
58 static pto_t *get_pto (ir_node*, pto_env_t*);
59 static void pto_call (ir_graph*, ir_node*, pto_env_t*);
60 static void pto_raise (ir_node*, pto_env_t*);
61 static void pto_load (ir_node*, pto_env_t*);
62 static void pto_store (ir_node*, pto_env_t*);
63 static void pto_method (ir_node*, pto_env_t*);
64 static void pto_end_block (ir_node*, pto_env_t*);
66 /* ===================================================
68 =================================================== */
69 /* Add values of the actual arguments to the formal arguments */
70 static int add_graph_args (ir_graph *graph, ir_node *call, pto_env_t *env)
73 type *meth = get_entity_type (get_irg_entity (graph));
74 ir_node **args = get_irg_proj_args (graph);
77 assert(op_Call == get_irn_op(call));
79 n_args = get_Call_n_params (call);
81 DBGPRINT (1, (stdout, "add_graph_args(/*todo*/): args of %s[%li] -> 0x%08x\n",
82 OPNAME (call), OPNUM (call), (int) graph));
84 for (i = 0; i < n_args; i ++) {
85 if (NULL != args [i]) {
86 if (mode_P == get_type_mode (get_method_param_type (meth, i))) {
87 ir_node *call_arg = get_Call_param (call, i);
88 pto_t *arg_pto = get_pto (call_arg, env);
89 pto_t *frm_pto = get_node_pto (args [i]);
94 change |= qset_insert_all (frm_pto->values, arg_pto->values);
96 DBGPRINT (2, (stdout, "add_graph_args(/*todo*/): arg [%i]: -> %s[%li] (%i) -> %s[%li] (%i)\n",
98 OPNAME (call_arg), OPNUM (call_arg),
100 OPNAME (args [i]), OPNUM (args [i]),
101 frm_pto->values->id));
109 /* Transfer the actual arguments to the formal arguments */
110 static void set_graph_args (ir_graph *graph, ir_node *call, pto_env_t *env)
112 type *meth = get_entity_type (get_irg_entity (graph));
113 ir_node **args = get_irg_proj_args (graph);
116 assert (op_Call == get_irn_op(call));
118 n_args = get_Call_n_params (call);
120 for (i = 0; i < n_args; i ++) {
121 if (NULL != args [i]) {
122 if (mode_P == get_type_mode (get_method_param_type (meth, i))) {
123 ir_node *call_arg = get_Call_param (call, i);
124 pto_t *pto = get_pto (call_arg, env);
126 set_node_pto (args [i], pto);
128 DBGPRINT (1, (stdout, "set_graph_args(/*todo*/): arg [%i]: %s[%li] -> %s[%li] (%i)\n",
130 OPNAME (call_arg), OPNUM (call_arg),
131 OPNAME (args [i]), OPNUM (args [i]),
138 /* Transfer the graph's result to the call */
139 static int set_graph_result (ir_graph *graph, ir_node *call)
141 type *tp = get_entity_type (get_irg_entity (graph));
143 pto_t *ret_pto, *call_pto;
146 if (0 == get_method_n_ress (tp)) {
150 tp = get_method_res_type (tp, 0);
152 if (mode_P != get_type_mode (tp)) {
153 set_node_pto (call, NULL);
158 end_block = get_irg_end_block (graph);
159 ret_pto = get_node_pto (end_block);
161 call_pto = get_node_pto (call);
165 DBGPRINT (0, (stdout, "set_graph_result(/*todo*/): before change args\n"));
166 DBGEXE (0, pto_print_pto (end_block));
167 DBGEXE (0, pto_print_pto (call));
169 change = qset_insert_all (call_pto->values, ret_pto->values);
172 DBGPRINT (0, (stdout, "set_graph_result(/*todo*/): after change args\n"));
173 DBGEXE (0, pto_print_pto (end_block));
174 DBGEXE (0, pto_print_pto (call));
181 /* Propagation of PTO values */
182 static pto_t *get_pto_proj (ir_node *proj, pto_env_t *env)
184 ir_node *proj_in = get_Proj_pred (proj);
185 const long proj_proj = get_Proj_proj (proj);
186 const opcode in_op = get_irn_opcode (proj_in);
187 pto_t *in_pto = NULL;
188 pto_t *proj_pto = NULL; /* get_node_pto (proj); */
190 ir_node *proj_in_in = NULL;
193 case (iro_Start): /* ProjT (Start) */
194 assert (0 && "pto from ProjT(Start) requested");
197 case (iro_Proj): { /* ProjT (Start), ProjT (Call) */
201 proj_in_in = get_Proj_pred (proj_in);
202 in_in_op = get_irn_opcode (proj_in_in);
203 proj_in_proj = get_Proj_proj (proj_in);
205 assert ((pn_Start_T_args == proj_in_proj) ||
206 (pn_Call_T_result == proj_in_proj));
209 case (iro_Start): /* ProjArg (ProjT (Start)) */
210 /* then the pto value must have been set to the node */
211 proj_pto = get_node_pto (proj);
215 case (iro_Call): /* ProjV (ProjT (Call)) */
216 if (NULL != proj_pto) {
219 in_pto = get_pto (proj_in, env);
220 set_node_pto (proj, in_pto);
226 default: assert (0 && "Proj(Proj(?))");
228 /* done with case (in_op == iro_Proj) */
231 case (iro_Load): /* ProjV (Load) */
232 assert (pn_Load_res == proj_proj);
234 case (iro_Call): /* ProjT (Call) */
236 case (iro_Alloc): /* ProjV (Alloc) */
237 if (NULL != proj_pto) {
240 in_pto = get_pto (proj_in, env);
243 set_node_pto (proj, in_pto);
247 fprintf (stderr, "get_pto_proj(/*todo*/): not handled: proj (%s[%li])\n",
248 get_op_name (get_irn_op (proj_in)),
249 get_irn_node_nr (proj_in));
250 assert (0 && "Proj(?)");
256 static pto_t *get_pto_phi (ir_node *phi, pto_env_t *env)
262 assert (mode_P == get_irn_mode (phi));
264 pto = get_node_pto (phi);
265 assert (pto); /* must be initialised */
267 n_ins = get_irn_arity (phi);
268 for (i = 0; i < n_ins; i ++) {
269 ir_node *in = get_irn_n (phi, i);
270 pto_t *in_pto = get_pto (in, env);
274 change |= qset_insert_all (pto->values, in_pto->values);
277 env->change |= change;
282 static pto_t *get_pto_sel (ir_node *sel, pto_env_t *env)
284 pto_t *pto = NULL; /* get_node_pto (sel); */
287 ir_node *in = get_Sel_ptr (sel);
289 pto = get_pto (in, env);
290 set_node_pto (sel, pto);
296 static pto_t *get_pto_ret (ir_node *ret, pto_env_t *env)
298 pto_t *pto = NULL; /* get_node_pto (ret); */
301 ir_node *in = get_Return_res (ret, 0);
303 pto = get_pto (in, env);
304 set_node_pto (ret, pto);
309 DBGPRINT (9, (stdout, "get_pto_ret(/*todo*/): "));
310 DBGEXE (9, pto_print_pto (ret));
316 /* Dispatch to propagate PTO values */
317 static pto_t *get_pto (ir_node *node, pto_env_t *env)
319 const opcode op = get_irn_opcode (node);
321 DBGPRINT (2, (stdout, "get_pto (%s[%li])\n",
322 OPNAME (node), OPNUM (node)));
325 case (iro_Cast): return (get_pto (get_Cast_op (node), env));
326 case (iro_Proj): return (get_pto_proj (node, env));
327 case (iro_Phi): return (get_pto_phi (node, env));
328 case (iro_Sel): return (get_pto_sel (node, env));
329 case (iro_Alloc): return (get_alloc_pto (node));
330 case (iro_Return): return (get_pto_ret (node, env));
332 case (iro_Call): /* FALLTHROUGH */
333 case (iro_Load): /* FALLTHROUGH */
334 case (iro_Const): /* FALLTHROUGH */
335 case (iro_SymConst):{
336 pto_t *pto = get_node_pto (node);
342 /* stopgap measure */
343 fprintf (stderr, "get_pto(/*todo*/): not handled: node[%li].op = %s\n",
344 get_irn_node_nr (node),
345 get_op_name (get_irn_op (node)));
346 assert (0 && "something not handled");
352 /* Actions for the nodes: */
353 static void pto_load (ir_node *load, pto_env_t *pto_env)
359 DBGPRINT (2, (stdout, "pto_load(/*todo*/) (%s[%li]): pto = 0x%08x\n",
360 OPNAME (load), OPNUM (load), (int) get_node_pto (load)));
362 ptr = get_Load_ptr (load);
364 if (is_dummy_load_ptr (ptr)) {
368 ent = get_ptr_ent (ptr);
370 if (mode_P == get_type_mode (get_entity_type (ent))) {
371 pto_t *ptr_pto = get_pto (ptr, pto_env);
375 DBGPRINT (1, (stdout, "pto_load(/*todo*/) (%s[%li]): ptr = 0x%08x\n",
376 OPNAME (ptr), OPNUM (ptr), (int) ptr_pto));
378 pto_env->change |= mod_load (load, ent, ptr_pto);
382 static void pto_store (ir_node *store, pto_env_t *pto_env)
386 pto_t *ptr_pto, *val_pto;
389 DBGPRINT (2, (stdout, "pto_store(/*todo*/) (%s[%li]) (no pto)\n",
390 OPNAME (store), OPNUM (store)));
392 ptr = get_Store_ptr (store);
393 val = get_Store_value (store);
395 if (mode_P != get_irn_mode (val)) {
399 ent = get_ptr_ent (ptr);
401 ptr_pto = get_pto (ptr, pto_env);
402 val_pto = get_pto (val, pto_env);
407 DBGPRINT (2, (stdout, "pto_store(/*todo*/) (%s[%li]): ptr_pto = 0x%08x\n",
408 OPNAME (ptr), OPNUM (ptr), (int) ptr_pto));
409 DBGPRINT (2, (stdout, "pto_store(/*todo*/) (%s[%li]): val_pto = 0x%08x\n",
410 OPNAME (val), OPNUM (val), (int) val_pto));
412 pto_env->change |= mod_store (store, ent, ptr_pto, val_pto);
415 static void pto_method (ir_node *call, pto_env_t *pto_env)
418 callEd_info_t *callEd_info;
420 DBGPRINT (2, (stdout, "pto_method(/*todo*/):%i (%s[%li]): pto = 0x%08x\n",
421 __LINE__, OPNAME (call), OPNUM (call),
422 (int) get_node_pto (call)));
424 callEd_info = ecg_get_callEd_info (call);
426 if (NULL == callEd_info) {
427 DBGPRINT (2, (stdout, "pto_method(/*todo*/):%i (%s[%li]), no graph\n",
428 __LINE__, OPNAME (call), OPNUM (call)));
432 while (NULL != callEd_info) {
433 DBGPRINT (2, (stdout, "pto_method(/*todo*/):%i (%s[%li]), graph %i\n",
434 __LINE__, OPNAME (call), OPNUM (call), i ++));
436 pto_call (callEd_info->callEd, call, pto_env);
438 callEd_info = callEd_info->prev;
442 /* Perform the appropriate action on the given node */
443 static void pto_node_node(ir_node *node, pto_env_t *pto_env)
445 opcode op = get_irn_opcode (node);
447 DBGPRINT (1, (stdout, "pto_node_node(/*todo*/) (%s[%li])\n",
448 OPNAME (node), OPNUM (node)));
451 case (iro_Start): /* nothing */ break;
453 pto_load (node, pto_env);
457 pto_store (node, pto_env);
461 pto_method (node, pto_env);
465 pto_raise (node, pto_env);
477 pto_end_block (node, pto_env);
482 assert (mode_M == get_irn_mode (node));
486 /* uninteresting stuff: */
490 case (iro_DivMod): /* nothing to do */ break;
493 /* stopgap measure */
494 fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
496 get_irn_node_nr (node),
497 get_op_name (get_irn_op (node)));
498 assert (0 && "something not handled");
503 /* Callback function to execute in pre-order */
504 static void pto_node_pre (ir_node *node, void *env)
509 /* Callback function to execute in post-order */
510 static void pto_node_post (ir_node *node, void *env)
512 pto_env_t *pto_env = (pto_env_t*) env;
514 DBGPRINT (999, (stdout, "%s (%s[%li])\n",
516 OPNAME (node), OPNUM (node)));
518 pto_node_node (node, pto_env);
521 /* Perform a single pass over the given graph */
522 static void pto_graph_pass (ir_graph *graph, void *pto_env)
524 irg_walk_mem (graph, pto_node_pre, pto_node_post, pto_env);
527 /* Continue PTO for one of the graphs called at a Call */
528 static void pto_call (ir_graph *graph, ir_node *call, pto_env_t *pto_env)
532 /* only for debugging stuff: */
533 entity *ent = get_irg_entity (graph);
534 const char *ent_name = (char*) get_entity_name (ent);
535 const char *own_name = (char*) get_type_name (get_entity_owner (ent));
538 DBGPRINT (2, (stdout, "%s (%s[%li]) to \"%s.%s\"\n",
540 OPNAME (call), OPNUM (call),
541 own_name, ent_name));
543 if (! get_irg_is_mem_visited (graph)) {
544 /* handle direct call */
545 graph_info_t *ginfo = ecg_get_info (graph);
548 int ctx_idx = find_ctx_idx (call, ginfo, get_curr_ctx ());
549 ctx_info_t *call_ctx = get_ctx (ginfo, ctx_idx);
550 ctx_info_t *old_ctx = set_curr_ctx (call_ctx);
552 DBGPRINT (1, (stdout, "%s>CTX: ", -- spaces));
553 DBGEXE (1, ecg_print_ctx (call_ctx, stdout));
555 /* Initialise Alloc Names and Node values */
556 pto_reset_graph_pto (graph, ctx_idx);
558 /* Compute Arguments */
559 set_graph_args (graph, call, pto_env);
561 /* Visit/Iterate Graph */
562 pto_graph (graph, ctx_idx, pto_env);
565 set_curr_ctx (old_ctx);
567 /* Get Return Value from Graph */
568 change |= set_graph_result (graph, call);
570 DBGPRINT (1, (stdout, "%s<CTX: ", spaces ++));
571 DBGEXE (1, ecg_print_ctx (call_ctx, stdout));
573 /* Don't need to reset alloc names unless we handle recursion here */
578 /* handle recursion */
579 DBGPRINT (0, (stdout, "%s: recursion into \"%s.%s\"\n",
581 own_name, ent_name));
582 /* Find 'right' enclosing pto_env */
584 while (graph != enc_env->graph) {
585 enc_env = enc_env->enc_env; /* talk about naming issues here */
587 /* since we're in a recursion loop, we *must* find an env for the callEd here: */
588 assert (NULL != enc_env->enc_env);
591 /* Re-Set arguments */
592 rec_change = add_graph_args (graph, call, pto_env);
594 DBGPRINT (1, (stdout, "%s: return in:", __FUNCTION__));
595 DBGEXE (1, pto_print_pto (get_irg_end_block (graph)));
598 DBGPRINT (0, (stdout, "%s: change args", __FUNCTION__));
601 rec_change |= set_graph_result (graph, call);
604 DBGPRINT (0, (stdout, "%s: return out:", __FUNCTION__));
605 DBGEXE (0, pto_print_pto (get_irg_end_block (graph)));
609 /* appears that we don't need this: */
610 enc_env->change |= rec_change;
614 /* Todo: Set 'Unknown' Value as Return Value when the graph is not
617 pto_env->change |= change;
620 static void pto_raise (ir_node *raise, pto_env_t *pto_env)
623 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = 0x%08x\n",
625 OPNAME (raise), OPNUM (raise), (int) get_node_pto (raise)));
628 static void pto_end_block (ir_node *end_block, pto_env_t *pto_env)
630 /* perform end block */
631 type *tp = get_entity_type (get_irg_entity (get_irn_irg (end_block)));
635 if (0 == get_method_n_ress (tp)) {
639 tp = get_method_res_type (tp, 0);
641 if (mode_P != get_type_mode (tp)) {
645 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = 0x%08x\n",
647 OPNAME (end_block), OPNUM (end_block),
648 (int) get_node_pto (end_block)));
650 end_pto = get_node_pto (end_block);
654 n_ins = get_irn_arity (end_block);
655 for (i = 0; i < n_ins; i ++) {
656 ir_node *in = get_irn_n (end_block, i);
658 if (iro_Return == get_irn_opcode (in)) {
659 pto_t *in_pto = get_pto (in, pto_env);
661 pto_env->change |= qset_insert_all (end_pto->values, in_pto->values);
666 /* ===================================================
667 Exported Implementation:
668 =================================================== */
669 /* Main loop: Initialise and iterate over the given graph */
670 void pto_graph (ir_graph *graph, int ctx_idx, pto_env_t *enc_env)
675 /* Also exported, since we need it in 'pto.c' */
676 pto_env = (pto_env_t*) xmalloc (sizeof (pto_env_t*));
677 pto_env->enc_env = enc_env;
678 pto_env->graph = graph;
679 pto_env->ctx_idx = ctx_idx;
680 pto_env->change = TRUE;
682 /* HERE ("start"); */
684 DBGPRINT (2, (stdout, "%s: start for ctx %i\n",
688 /* todo (here): iterate, obey 'changed' attribute */
690 while (0 != pto_env->change) {
692 pto_env->change = FALSE;
693 pto_graph_pass (graph, pto_env);
696 DBGPRINT (1, (stdout, "%s: %i runs on \"%s.%s\"\n",
699 get_type_name (get_entity_owner (get_irg_entity (graph))),
700 get_entity_name (get_irg_entity (graph))));
701 memset (pto_env, 0, sizeof(*pto_env));
706 /* Set the PTO value for the given non-alloc node */
707 void set_node_pto (ir_node *node, pto_t *pto)
709 assert (op_Alloc != get_irn_op(node));
711 set_irn_link (node, (void*) pto);
714 /*Get the PTO value for the given non-alloc node */
715 pto_t *get_node_pto (ir_node *node)
717 assert (op_Alloc != get_irn_op(node));
719 return ((pto_t*) get_irn_link (node));
722 /* Set the PTO value for the given alloc node */
723 void set_alloc_pto (ir_node *alloc, alloc_pto_t *alloc_pto)
725 assert (op_Alloc == get_irn_op(alloc));
729 set_irn_link (alloc, (void*) alloc_pto);
732 /*Get the current PTO value for the given alloc node */
733 pto_t *get_alloc_pto (ir_node *alloc)
735 alloc_pto_t *alloc_pto = get_irn_link (alloc);
737 assert (op_Alloc == get_irn_op(alloc));
739 assert (alloc_pto -> curr_pto);
741 return (alloc_pto -> curr_pto);
747 Revision 1.13 2005/01/10 17:26:34 liekweg
748 fixup printfs, don't put environments on the stack
750 Revision 1.12 2004/12/23 15:46:19 beck
751 removed unneeded allocations
753 Revision 1.11 2004/12/21 14:50:59 beck
754 removed C)) and GNUC constructs, add default returns
756 Revision 1.10 2004/12/20 17:34:35 liekweg
757 fix recursion handling
759 Revision 1.9 2004/12/15 13:31:18 liekweg
760 remove debugging stuff
762 Revision 1.8 2004/12/15 09:18:18 liekweg
765 Revision 1.7 2004/12/06 12:55:06 liekweg
768 Revision 1.6 2004/12/02 16:17:51 beck
769 fixed config.h include
771 Revision 1.5 2004/11/30 14:47:54 liekweg
772 fix initialisation; do correct iteration
774 Revision 1.4 2004/11/26 15:59:40 liekweg
775 verify pto_{load,store}
777 Revision 1.3 2004/11/24 14:53:55 liekweg
780 Revision 1.2 2004/11/20 21:21:56 liekweg
781 Finalise initialisation
783 Revision 1.1 2004/11/18 16:37:34 liekweg