fixup printfs, don't put environments on the stack
[libfirm] / ir / ana2 / pto_util.c
1 /* -*- c -*- */
2
3 /*
4    Project:     libFIRM
5    File name:   ir/ana/pto_util.c
6    Purpose:     Utilitites for PTO
7    Author:      Florian
8    Modified by:
9    Created:     Sat Nov 13 19:35:27 CET 2004
10    CVS-ID:      $Id$
11    Copyright:   (c) 1999-2004 Universität Karlsruhe
12    Licence:     This file is protected by the GPL -  GNU GENERAL PUBLIC LICENSE.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 /*
20  pto_util: Utilitites for PTO
21 */
22
23 # include "pto_util.h"
24
25 # include "irnode_t.h"
26 # include "irgwalk.h"
27 # include "xmalloc.h"
28
29 # include "pto_debug.h"
30
31 /* Local Defines: */
32 # ifndef TRUE
33 #  define TRUE 1
34 #  define FALSE 0
35 # endif /* not defined TRUE */
36
37 /* Local Data Types: */
38 /* Environment for find_irg_args */
39 typedef struct find_irg_args_env {
40   ir_node **args;
41   ir_node *arg;
42 } find_irg_args_env_t;
43
44
45 /* Local Variables: */
46
47 /* Local Prototypes: */
48
49 /* ===================================================
50    Local Implementation:
51    =================================================== */
52 /* Helper for find_irg_args */
53 static void find_irg_arg (ir_node *node, void *env)
54 {
55   find_irg_args_env_t *arg_env = (find_irg_args_env_t*) env;
56
57   if (iro_Proj == get_irn_opcode (node)) {
58     if (arg_env->arg == get_Proj_pred (node)) {
59       long n = get_Proj_proj (node);
60
61       assert (! arg_env->args [n]);
62
63       arg_env->args [n] = node;
64     }
65   }
66 }
67
68 /* ===================================================
69    Exported Implementation:
70    =================================================== */
71 /* Find the arguments of a graph. For a method that has n args, the
72   result array has 'n+1' entries, the last of which is written NULL.
73   Note that not all entries in [0..n-1] will be populated all the time.
74 */
75 ir_node **find_irg_args (ir_graph *graph)
76 {
77   type *tp = get_entity_type (get_irg_entity (graph));
78   const int n_args = get_method_n_params (tp);
79   ir_node **args = xcalloc (n_args + 1, sizeof (ir_node*));
80   ir_node *arg = get_irg_args (graph);
81   find_irg_args_env_t *arg_env;
82
83   arg_env = (find_irg_args_env_t*) xmalloc (sizeof (find_irg_args_env_t));
84
85   arg_env->args = args;
86   arg_env->arg  = arg;
87
88   {
89     ir_graph *save = get_current_ir_graph ();
90
91     set_current_ir_graph (graph);
92     irg_walk (get_irg_end (graph), find_irg_arg, NULL, arg_env);
93     set_current_ir_graph (save);
94   }
95
96    memset (arg_env, 0x00, sizeof (find_irg_args_env_t));
97    free (arg_env);
98
99   args [n_args] = NULL;
100
101   return (args);
102 }
103
104 /* Get the entity of a ptr */
105 entity *get_ptr_ent (ir_node *ptr)
106 {
107   entity *ent = NULL;
108   const opcode ptr_op = get_irn_opcode (ptr);
109   switch (ptr_op) {
110   case (iro_Cast): {
111     ent = get_ptr_ent (get_Cast_op (ptr));
112   } break;
113   case (iro_Sel): {
114     ent = get_Sel_entity (ptr);
115   } break;
116
117   case (iro_SymConst): {
118     ent = get_SymConst_entity (ptr);
119   } break;
120
121   default: {
122     fprintf (stderr, "get_ptr_ent: no ent for ptr=%s[%ld]\n",
123              get_op_name (get_irn_op (ptr)),
124              get_irn_node_nr (ptr));
125     assert (0);
126   }
127   }
128
129   return (ent);
130 }
131
132 /* Check whether the load of the given ptr is a dummy */
133 int is_dummy_load_ptr (ir_node *ptr)
134 {
135   const opcode ptr_op = get_irn_opcode (ptr);
136
137   switch (ptr_op) {
138   case (iro_Cast): {
139     return (is_dummy_load_ptr (get_Cast_op (ptr)));
140   } break;
141   case (iro_Sel):
142   case (iro_SymConst): {
143     return (FALSE);
144   } break;
145
146   default: {
147     return (TRUE);
148   }
149   }
150 }
151
152 \f
153 /*
154   $Log$
155   Revision 1.15  2005/01/10 17:26:34  liekweg
156   fixup printfs, don't put environments on the stack
157
158   Revision 1.14  2004/12/23 15:47:09  beck
159   removed uneeded allocations
160   used new xcalloc
161
162   Revision 1.13  2004/12/22 14:43:14  beck
163   made allocations C-like
164
165   Revision 1.12  2004/12/21 15:53:12  beck
166   removed GNUC constructs
167
168   Revision 1.11  2004/12/20 17:34:35  liekweg
169   fix recursion handling
170
171   Revision 1.10  2004/12/06 12:55:06  liekweg
172   actually iterate
173
174   Revision 1.9  2004/12/02 16:17:51  beck
175   fixed config.h include
176
177   Revision 1.8  2004/11/26 15:59:14  liekweg
178   recognize dummy loads
179
180   Revision 1.7  2004/11/24 14:53:56  liekweg
181   Bugfixes
182
183   Revision 1.6  2004/11/18 16:37:07  liekweg
184   rewrite
185
186
187 */