fix
[libfirm] / ir / ana2 / pto_util.c
1 /* -*- c -*- */
2
3 /*
4  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
5  *
6  * This file is part of libFirm.
7  *
8  * This file may be distributed and/or modified under the terms of the
9  * GNU General Public License version 2 as published by the Free Software
10  * Foundation and appearing in the file LICENSE.GPL included in the
11  * packaging of this file.
12  *
13  * Licensees holding valid libFirm Professional Edition licenses may use
14  * this file in accordance with the libFirm Commercial License.
15  * Agreement provided with the Software.
16  *
17  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE.
20  */
21
22 /**
23  * @file
24  * @brief    Utilitites for PTO
25  * @author   Florian
26  * @date     Sat Nov 13 19:35:27 CET 2004
27  * @version  $Id$
28  */
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 /*
34  pto_util: Utilitites for PTO
35 */
36
37 # include "pto_util.h"
38
39 # include "irnode_t.h"
40 # include "irgwalk.h"
41 # include "xmalloc.h"
42
43 # include "pto_debug.h"
44 # include "gnu_ext.h"
45
46 /* Local Defines: */
47 # ifndef TRUE
48 #  define TRUE 1
49 #  define FALSE 0
50 # endif /* not defined TRUE */
51
52 /* Local Data Types: */
53 /* Environment for find_irg_args */
54 typedef struct find_irg_args_env {
55   ir_node **args;
56   ir_node *arg;
57 } find_irg_args_env_t;
58
59
60 /* Local Variables: */
61
62 /* Local Prototypes: */
63
64 /* ===================================================
65    Local Implementation:
66    =================================================== */
67 /* Helper for find_irg_args */
68 static void find_irg_arg (ir_node *node, void *env)
69 {
70   find_irg_args_env_t *arg_env = (find_irg_args_env_t*) env;
71
72   if (iro_Proj == get_irn_opcode (node)) {
73     if (arg_env->arg == get_Proj_pred (node)) {
74       long n = get_Proj_proj (node);
75
76       assert (! arg_env->args [n]);
77
78       arg_env->args [n] = node;
79     }
80   }
81 }
82
83 /* ===================================================
84    Exported Implementation:
85    =================================================== */
86 /* Find the arguments of a graph. For a method that has n args, the
87   result array has 'n+1' entries, the last of which is written NULL.
88   Note that not all entries in [0..n-1] will be populated all the time.
89 */
90 ir_node **find_irg_args (ir_graph *graph)
91 {
92   ir_type *tp = get_entity_type (get_irg_entity (graph));
93   const int n_args = get_method_n_params (tp);
94   ir_node **args = xcalloc (n_args + 1, sizeof (ir_node*));
95   ir_node *arg = get_irg_args (graph);
96   find_irg_args_env_t *arg_env;
97
98   arg_env = (find_irg_args_env_t*) xmalloc (sizeof (find_irg_args_env_t));
99
100   arg_env->args = args;
101   arg_env->arg  = arg;
102
103   {
104     ir_graph *save = get_current_ir_graph ();
105
106     set_current_ir_graph (graph);
107     irg_walk (get_irg_end (graph), find_irg_arg, NULL, arg_env);
108     set_current_ir_graph (save);
109   }
110
111    memset (arg_env, 0x00, sizeof (find_irg_args_env_t));
112    free (arg_env);
113
114   args [n_args] = NULL;
115
116   return (args);
117 }
118
119 /* Get the entity of a ptr */
120 ir_entity *get_ptr_ent (ir_node *ptr)
121 {
122   ir_entity *ent = NULL;
123   const ir_opcode ptr_op = get_irn_opcode (ptr);
124   switch (ptr_op) {
125   case (iro_Cast): {
126     ent = get_ptr_ent (get_Cast_op (ptr));
127   } break;
128   case (iro_Sel): {
129     ent = get_Sel_entity (ptr);
130   } break;
131
132   case (iro_SymConst): {
133     ent = get_SymConst_entity (ptr);
134   } break;
135
136   default: {
137     fprintf (stderr, "%s: no ent for ptr=%s[%ld]\n",
138              __FUNCTION__,
139              get_op_name (get_irn_op (ptr)),
140              get_irn_node_nr (ptr));
141     assert (0);
142   }
143   }
144
145   return (ent);
146 }
147
148 /* Check whether the load of the given ptr is a dummy */
149 int is_dummy_load_ptr (ir_node *ptr)
150 {
151   const ir_opcode ptr_op = get_irn_opcode (ptr);
152
153   switch (ptr_op) {
154   case (iro_Cast): {
155     return (is_dummy_load_ptr (get_Cast_op (ptr)));
156   } break;
157   case (iro_Sel):
158   case (iro_SymConst): {
159     return (FALSE);
160   } break;
161
162   default: {
163     return (TRUE);
164   }
165   }
166 }
167
168 \f
169 /*
170   $Log$
171   Revision 1.19  2007/01/16 15:45:42  beck
172   renamed type opcode to ir_opcode
173
174   Revision 1.18  2006/12/13 19:46:47  beck
175   rename type entity into ir_entity
176
177   Revision 1.17  2006/06/08 10:49:07  beck
178   renamed type to ir_type
179
180   Revision 1.16  2005/01/14 14:13:32  liekweg
181   fix gnu extension
182
183   Revision 1.15  2005/01/10 17:26:34  liekweg
184   fixup printfs, don't put environments on the stack
185
186   Revision 1.14  2004/12/23 15:47:09  beck
187   removed uneeded allocations
188   used new xcalloc
189
190   Revision 1.13  2004/12/22 14:43:14  beck
191   made allocations C-like
192
193   Revision 1.12  2004/12/21 15:53:12  beck
194   removed GNUC constructs
195
196   Revision 1.11  2004/12/20 17:34:35  liekweg
197   fix recursion handling
198
199   Revision 1.10  2004/12/06 12:55:06  liekweg
200   actually iterate
201
202   Revision 1.9  2004/12/02 16:17:51  beck
203   fixed config.h include
204
205   Revision 1.8  2004/11/26 15:59:14  liekweg
206   recognize dummy loads
207
208   Revision 1.7  2004/11/24 14:53:56  liekweg
209   Bugfixes
210
211   Revision 1.6  2004/11/18 16:37:07  liekweg
212   rewrite
213
214
215 */