5 File name: ir/ana2/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"
32 # include "irprog_t.h"
34 # include "irmemwalk.h"
36 # include "pto_debug.h"
37 # include "pto_init.h"
45 /* Local Data Types: */
46 typedef struct pto_env_str {
47 struct pto_env_str *enc_env;
54 /* Local Variables: */
59 /* Local Prototypes: */
60 static pto_t *get_pto (ir_node*, pto_env_t*);
61 static void pto_call (ir_graph*, ir_node*, pto_env_t*);
62 static void pto_raise (ir_node*, pto_env_t*);
63 static void pto_load (ir_node*, pto_env_t*);
64 static void pto_store (ir_node*, pto_env_t*);
65 static void pto_method (ir_node*, pto_env_t*);
66 static void pto_end_block (ir_node*, pto_env_t*);
68 /* ===================================================
70 =================================================== */
71 /* Add values of the actual arguments to the formal arguments */
72 static int add_graph_args (ir_graph *graph, ir_node *call, pto_env_t *env)
75 ir_type *meth = get_entity_type (get_irg_entity (graph));
76 ir_node **args = get_irg_proj_args (graph);
79 assert(op_Call == get_irn_op(call));
81 n_args = get_Call_n_params (call);
83 DBGPRINT (1, (stdout, "%s: args of %s[%li] -> 0x%08x\n",
85 OPNAME (call), OPNUM (call), (int) graph));
87 for (i = 0; i < n_args; i ++) {
88 if (NULL != args [i]) {
89 if (mode_P == get_type_mode (get_method_param_type (meth, i))) {
90 ir_node *call_arg = get_Call_param (call, i);
91 pto_t *arg_pto = get_pto (call_arg, env);
92 pto_t *frm_pto = get_node_pto (args [i]);
97 change |= qset_insert_all (frm_pto->values, arg_pto->values);
99 DBGPRINT (2, (stdout, "%s: arg [%i]: -> %s[%li] (%i) -> %s[%li] (%i)\n",
102 OPNAME (call_arg), OPNUM (call_arg),
104 OPNAME (args [i]), OPNUM (args [i]),
105 frm_pto->values->id));
113 /* Transfer the actual arguments to the formal arguments */
114 static void set_graph_args (ir_graph *graph, ir_node *call, pto_env_t *env)
116 ir_type *meth = get_entity_type (get_irg_entity (graph));
117 ir_node **args = get_irg_proj_args (graph);
120 assert (op_Call == get_irn_op(call));
122 n_args = get_Call_n_params (call);
124 for (i = 0; i < n_args; i ++) {
125 if (NULL != args [i]) {
126 if (mode_P == get_type_mode (get_method_param_type (meth, i))) {
127 ir_node *call_arg = get_Call_param (call, i);
128 pto_t *pto = get_pto (call_arg, env);
130 set_node_pto (args [i], pto);
132 DBGPRINT (1, (stdout, "%s: arg [%i]: %s[%li] -> %s[%li] (%i)\n",
135 OPNAME (call_arg), OPNUM (call_arg),
136 OPNAME (args [i]), OPNUM (args [i]),
143 /* Transfer the graph's result to the call */
144 static int set_graph_result (ir_graph *graph, ir_node *call)
146 ir_type *tp = get_entity_type (get_irg_entity (graph));
148 pto_t *ret_pto, *call_pto;
151 if (0 == get_method_n_ress (tp)) {
155 tp = get_method_res_type (tp, 0);
157 if (mode_P != get_type_mode (tp)) {
158 set_node_pto (call, NULL);
163 end_block = get_irg_end_block (graph);
164 ret_pto = get_node_pto (end_block);
166 call_pto = get_node_pto (call);
170 DBGPRINT (1, (stdout, "%s: before change args\n", __FUNCTION__));
171 DBGEXE (1, pto_print_pto (end_block));
172 DBGEXE (1, pto_print_pto (call));
174 change = qset_insert_all (call_pto->values, ret_pto->values);
177 DBGPRINT (1, (stdout, "%s: after change args\n", __FUNCTION__));
178 DBGEXE (1, pto_print_pto (end_block));
179 DBGEXE (1, pto_print_pto (call));
186 /* Propagation of PTO values */
187 static pto_t *get_pto_proj (ir_node *proj, pto_env_t *env)
189 ir_node *proj_in = get_Proj_pred (proj);
190 const ir_opcode in_op = get_irn_opcode (proj_in);
191 pto_t *in_pto = NULL;
192 pto_t *proj_pto = NULL; /* get_node_pto (proj); */
194 ir_node *proj_in_in = NULL;
197 case (iro_Start): /* ProjT (Start) */
198 assert (0 && "pto from ProjT(Start) requested");
201 case (iro_Proj): { /* ProjT (Start), ProjT (Call) */
205 proj_in_in = get_Proj_pred (proj_in);
206 in_in_op = get_irn_opcode (proj_in_in);
207 proj_in_proj = get_Proj_proj (proj_in);
209 assert ((pn_Start_T_args == proj_in_proj) ||
210 (pn_Call_T_result == proj_in_proj));
213 case (iro_Start): /* ProjArg (ProjT (Start)) */
214 /* then the pto value must have been set to the node */
215 proj_pto = get_node_pto (proj);
219 case (iro_Call): /* ProjV (ProjT (Call)) */
220 if (NULL != proj_pto) {
223 in_pto = get_pto (proj_in, env);
224 set_node_pto (proj, in_pto);
230 default: assert (0 && "Proj(Proj(?))");
232 /* done with case (in_op == iro_Proj) */
235 case (iro_Load): /* ProjV (Load) */
236 assert (pn_Load_res == get_Proj_proj(proj));
238 case (iro_Call): /* ProjT (Call) */
240 case (iro_Alloc): /* ProjV (Alloc) */
241 if (NULL != proj_pto) {
244 in_pto = get_pto (proj_in, env);
247 set_node_pto (proj, in_pto);
251 fprintf (stderr, "get_pto_proj(/*todo*/): not handled: proj (%s[%li])\n",
252 get_op_name (get_irn_op (proj_in)),
253 get_irn_node_nr (proj_in));
254 assert (0 && "Proj(?)");
260 static pto_t *get_pto_phi (ir_node *phi, pto_env_t *env)
266 assert (mode_P == get_irn_mode (phi));
268 pto = get_node_pto (phi);
269 assert (pto); /* must be initialised */
271 n_ins = get_irn_arity (phi);
272 for (i = 0; i < n_ins; i ++) {
273 ir_node *in = get_irn_n (phi, i);
274 pto_t *in_pto = get_pto (in, env);
278 change |= qset_insert_all (pto->values, in_pto->values);
281 env->change |= change;
286 static pto_t *get_pto_sel (ir_node *sel, pto_env_t *env)
288 pto_t *pto = NULL; /* get_node_pto (sel); */
291 ir_node *in = get_Sel_ptr (sel);
293 pto = get_pto (in, env);
294 set_node_pto (sel, pto);
300 static pto_t *get_pto_ret (ir_node *ret, pto_env_t *env)
302 pto_t *pto = NULL; /* get_node_pto (ret); */
305 ir_node *in = get_Return_res (ret, 0);
307 pto = get_pto (in, env);
308 set_node_pto (ret, pto);
313 DBGPRINT (9, (stdout, "%s: ", __FUNCTION__));
314 DBGEXE (9, pto_print_pto (ret));
320 /* Dispatch to propagate PTO values */
321 static pto_t *get_pto (ir_node *node, pto_env_t *env)
323 const ir_opcode op = get_irn_opcode (node);
325 DBGPRINT (2, (stdout, "%s (%s[%li])\n",
327 OPNAME (node), OPNUM (node)));
330 case (iro_Cast): return (get_pto (get_Cast_op (node), env));
331 case (iro_Proj): return (get_pto_proj (node, env));
332 case (iro_Phi): return (get_pto_phi (node, env));
333 case (iro_Sel): return (get_pto_sel (node, env));
334 case (iro_Alloc): return (get_alloc_pto (node));
335 case (iro_Return): return (get_pto_ret (node, env));
337 case (iro_Call): /* FALLTHROUGH */
338 case (iro_Load): /* FALLTHROUGH */
339 case (iro_Const): /* FALLTHROUGH */
340 case (iro_SymConst):{
341 pto_t *pto = get_node_pto (node);
347 /* stopgap measure */
348 fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
350 get_irn_node_nr (node),
351 get_op_name (get_irn_op (node)));
352 assert (0 && "something not handled");
358 /* Actions for the nodes: */
359 static void pto_load (ir_node *load, pto_env_t *pto_env)
365 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = 0x%08x\n",
367 OPNAME (load), OPNUM (load), (int) get_node_pto (load)));
369 ptr = get_Load_ptr (load);
371 if (is_dummy_load_ptr (ptr)) {
375 ent = get_ptr_ent (ptr);
377 if (mode_P == get_type_mode (get_entity_type (ent))) {
378 pto_t *ptr_pto = get_pto (ptr, pto_env);
382 DBGPRINT (1, (stdout, "%s (%s[%li]): ptr = 0x%08x\n",
384 OPNAME (ptr), OPNUM (ptr), (int) ptr_pto));
386 pto_env->change |= mod_load (load, ent, ptr_pto);
390 static void pto_store (ir_node *store, pto_env_t *pto_env)
394 pto_t *ptr_pto, *val_pto;
397 DBGPRINT (2, (stdout, "%s (%s[%li]) (no pto)\n",
399 OPNAME (store), OPNUM (store)));
401 ptr = get_Store_ptr (store);
402 val = get_Store_value (store);
404 if (mode_P != get_irn_mode (val)) {
408 ent = get_ptr_ent (ptr);
410 ptr_pto = get_pto (ptr, pto_env);
411 val_pto = get_pto (val, pto_env);
416 DBGPRINT (2, (stdout, "%s (%s[%li]): ptr_pto = 0x%08x\n",
418 OPNAME (ptr), OPNUM (ptr), (int) ptr_pto));
419 DBGPRINT (2, (stdout, "%s (%s[%li]): val_pto = 0x%08x\n",
421 OPNAME (val), OPNUM (val), (int) val_pto));
423 pto_env->change |= mod_store (store, ent, ptr_pto, val_pto);
426 static void pto_method (ir_node *call, pto_env_t *pto_env)
429 callEd_info_t *callEd_info;
431 DBGPRINT (2, (stdout, "%s:%i (%s[%li]): pto = 0x%08x\n",
432 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call),
433 (int) get_node_pto (call)));
435 callEd_info = ecg_get_callEd_info (call);
437 if (NULL == callEd_info) {
438 DBGPRINT (2, (stdout, "%s:%i (%s[%li]), no graph\n",
439 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call)));
443 while (NULL != callEd_info) {
444 DBGPRINT (2, (stdout, "%s:%i (%s[%li]), graph %i\n",
445 __FUNCTION__, __LINE__, OPNAME (call), OPNUM (call), i ++));
447 pto_call (callEd_info->callEd, call, pto_env);
449 callEd_info = callEd_info->prev;
453 /* Perform the appropriate action on the given node */
454 static void pto_node_node(ir_node *node, pto_env_t *pto_env)
456 ir_opcode op = get_irn_opcode (node);
458 DBGPRINT (1, (stdout, "%s (%s[%li])\n",
459 __FUNCTION__, OPNAME (node), OPNUM (node)));
462 case (iro_Start): /* nothing */ break;
464 pto_load (node, pto_env);
468 pto_store (node, pto_env);
472 pto_method (node, pto_env);
476 pto_raise (node, pto_env);
488 pto_end_block (node, pto_env);
493 assert (mode_M == get_irn_mode (node));
497 /* uninteresting stuff: */
501 case (iro_DivMod): /* nothing to do */ break;
504 /* stopgap measure */
505 fprintf (stderr, "%s: not handled: node[%li].op = %s\n",
507 get_irn_node_nr (node),
508 get_op_name (get_irn_op (node)));
509 assert (0 && "something not handled");
514 /* Callback function to execute in pre-order */
515 static void pto_node_pre (ir_node *node, void *env)
520 /* Callback function to execute in post-order */
521 static void pto_node_post (ir_node *node, void *env)
523 pto_env_t *pto_env = (pto_env_t*) env;
525 DBGPRINT (999, (stdout, "%s (%s[%li])\n",
527 OPNAME (node), OPNUM (node)));
529 pto_node_node (node, pto_env);
532 /* Perform a single pass over the given graph */
533 static void pto_graph_pass (ir_graph *graph, void *pto_env)
535 irg_walk_mem (graph, pto_node_pre, pto_node_post, pto_env);
538 /* Continue PTO for one of the graphs called at a Call */
539 static void pto_call (ir_graph *graph, ir_node *call, pto_env_t *pto_env)
543 /* only for debugging stuff: */
544 ir_entity *ent = get_irg_entity (graph);
545 const char *ent_name = (char*) get_entity_name (ent);
546 const char *own_name = (char*) get_type_name (get_entity_owner (ent));
549 DBGPRINT (2, (stdout, "%s (%s[%li]) to \"%s.%s\"\n",
551 OPNAME (call), OPNUM (call),
552 own_name, ent_name));
554 if (! get_irg_is_mem_visited (graph)) {
555 /* handle direct call */
556 graph_info_t *ginfo = ecg_get_info (graph);
559 int ctx_idx = find_ctx_idx (call, ginfo, get_curr_ctx ());
560 ctx_info_t *call_ctx = get_ctx (ginfo, ctx_idx);
561 ctx_info_t *old_ctx = set_curr_ctx (call_ctx);
563 DBGPRINT (1, (stdout, "%s>CTX: ", -- spaces));
564 DBGEXE (1, ecg_print_ctx (call_ctx, stdout));
566 /* Initialise Alloc Names and Node values */
567 pto_reset_graph_pto (graph, ctx_idx);
569 /* Compute Arguments */
570 set_graph_args (graph, call, pto_env);
572 /* Visit/Iterate Graph */
573 pto_graph (graph, ctx_idx, pto_env);
576 set_curr_ctx (old_ctx);
578 /* Get Return Value from Graph */
579 change |= set_graph_result (graph, call);
581 DBGPRINT (1, (stdout, "%s<CTX: ", spaces ++));
582 DBGEXE (1, ecg_print_ctx (call_ctx, stdout));
584 /* Don't need to reset alloc names unless we handle recursion here */
589 /* handle recursion */
590 DBGPRINT (0, (stdout, "%s: recursion into \"%s.%s\"\n",
592 own_name, ent_name));
593 /* Find 'right' enclosing pto_env */
595 while (graph != enc_env->graph) {
596 enc_env = enc_env->enc_env; /* talk about naming issues here */
598 /* since we're in a recursion loop, we *must* find an env for the callEd here: */
599 assert (NULL != enc_env->enc_env);
602 /* Re-Set arguments */
603 rec_change = add_graph_args (graph, call, pto_env);
605 DBGPRINT (1, (stdout, "%s: return in:", __FUNCTION__));
606 DBGEXE (1, pto_print_pto (get_irg_end_block (graph)));
609 DBGPRINT (0, (stdout, "%s: change args\n", __FUNCTION__));
612 rec_change |= set_graph_result (graph, call);
615 DBGPRINT (1, (stdout, "%s: return out:", __FUNCTION__));
616 DBGEXE (1, pto_print_pto (get_irg_end_block (graph)));
620 /* appears that we don't need this: */
621 enc_env->change |= rec_change;
625 /* Todo: Set 'Unknown' Value as Return Value when the graph is not
628 pto_env->change |= change;
631 static void pto_raise (ir_node *raise, pto_env_t *pto_env)
634 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = 0x%08x\n",
636 OPNAME (raise), OPNUM (raise), (int) get_node_pto (raise)));
639 static void pto_end_block (ir_node *end_block, pto_env_t *pto_env)
641 /* perform end block */
642 ir_type *tp = get_entity_type (get_irg_entity (get_irn_irg (end_block)));
646 if (0 == get_method_n_ress (tp)) {
650 tp = get_method_res_type (tp, 0);
652 if (! mode_is_reference(get_type_mode (tp))) {
656 DBGPRINT (2, (stdout, "%s (%s[%li]): pto = 0x%08x\n",
658 OPNAME (end_block), OPNUM (end_block),
659 (int) get_node_pto (end_block)));
661 end_pto = get_node_pto (end_block);
665 n_ins = get_irn_arity (end_block);
666 for (i = 0; i < n_ins; i ++) {
667 ir_node *in = get_irn_n (end_block, i);
669 if (iro_Return == get_irn_opcode (in)) {
670 pto_t *in_pto = get_pto (in, pto_env);
672 pto_env->change |= qset_insert_all (end_pto->values, in_pto->values);
677 /* ===================================================
678 Exported Implementation:
679 =================================================== */
680 /* Main loop: Initialise and iterate over the given graph */
681 void pto_graph (ir_graph *graph, int ctx_idx, pto_env_t *enc_env)
686 /* Also exported, since we need it in 'pto.c' */
687 pto_env = xmalloc (sizeof (pto_env_t));
688 pto_env->enc_env = enc_env;
689 pto_env->graph = graph;
690 pto_env->ctx_idx = ctx_idx;
691 pto_env->change = TRUE;
693 /* HERE ("start"); */
695 DBGPRINT (2, (stdout, "%s: start for ctx %i\n",
699 /* todo (here): iterate, obey 'changed' attribute */
701 while (0 != pto_env->change) {
703 pto_env->change = FALSE;
704 pto_graph_pass (graph, pto_env);
707 DBGPRINT (1, (stdout, "%s: %i runs on \"%s.%s\"\n",
710 get_type_name (get_entity_owner (get_irg_entity (graph))),
711 get_entity_name (get_irg_entity (graph))));
712 memset (pto_env, 0, sizeof (pto_env_t));
717 /* Set the PTO value for the given non-alloc node */
718 void set_node_pto (ir_node *node, pto_t *pto)
720 assert (op_Alloc != get_irn_op(node));
722 set_irn_link (node, (void*) pto);
725 /*Get the PTO value for the given non-alloc node */
726 pto_t *get_node_pto (ir_node *node)
728 assert (op_Alloc != get_irn_op(node));
730 return ((pto_t*) get_irn_link (node));
733 /* Set the PTO value for the given alloc node */
734 void set_alloc_pto (ir_node *alloc, alloc_pto_t *alloc_pto)
736 assert (op_Alloc == get_irn_op(alloc));
740 set_irn_link (alloc, (void*) alloc_pto);
743 /*Get the current PTO value for the given alloc node */
744 pto_t *get_alloc_pto (ir_node *alloc)
746 alloc_pto_t *alloc_pto = get_irn_link (alloc);
748 assert (op_Alloc == get_irn_op(alloc));
750 assert (alloc_pto -> curr_pto);
752 return (alloc_pto -> curr_pto);
758 Revision 1.21 2007/03/22 10:39:33 matze
759 a bunch of fixes to make firm work with NDEBUG and without DEBUG_libfirm
761 Revision 1.20 2007/01/16 15:45:42 beck
762 renamed type opcode to ir_opcode
764 Revision 1.19 2006/12/13 19:46:47 beck
765 rename type entity into ir_entity
767 Revision 1.18 2006/01/13 22:57:41 beck
768 renamed all types 'type' to 'ir_type'
769 used mode_is_reference instead of != mode_P test
771 Revision 1.17 2005/02/25 16:48:21 liekweg
774 Revision 1.16 2005/01/27 15:51:19 liekweg
777 Revision 1.15 2005/01/14 14:14:26 liekweg
778 fix gnu extension, fix fprintf's
780 Revision 1.14 2005/01/14 13:37:26 liekweg
781 fix allocation (size); don't cast malloc
783 Revision 1.13 2005/01/10 17:26:34 liekweg
784 fixup printfs, don't put environments on the stack
786 Revision 1.12 2004/12/23 15:46:19 beck
787 removed unneeded allocations
789 Revision 1.11 2004/12/21 14:50:59 beck
790 removed C)) and GNUC constructs, add default returns
792 Revision 1.10 2004/12/20 17:34:35 liekweg
793 fix recursion handling
795 Revision 1.9 2004/12/15 13:31:18 liekweg
796 remove debugging stuff
798 Revision 1.8 2004/12/15 09:18:18 liekweg
801 Revision 1.7 2004/12/06 12:55:06 liekweg
804 Revision 1.6 2004/12/02 16:17:51 beck
805 fixed config.h include
807 Revision 1.5 2004/11/30 14:47:54 liekweg
808 fix initialisation; do correct iteration
810 Revision 1.4 2004/11/26 15:59:40 liekweg
811 verify pto_{load,store}
813 Revision 1.3 2004/11/24 14:53:55 liekweg
816 Revision 1.2 2004/11/20 21:21:56 liekweg
817 Finalise initialisation
819 Revision 1.1 2004/11/18 16:37:34 liekweg