OSDN Git Service

f5855f2a2e554e9b8bd6389c9edea8d35c111c56
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-operands.h
1 /* SSA operand management for trees.
2    Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #ifndef GCC_TREE_SSA_OPERANDS_H
21 #define GCC_TREE_SSA_OPERANDS_H
22
23 /* Interface to SSA operands.  */
24
25
26 /* This represents a pointer to a DEF operand.  */
27 typedef tree *def_operand_p;
28
29 /* This represents a pointer to a USE operand.  */
30 typedef ssa_use_operand_t *use_operand_p;
31
32 /* NULL operand types.  */
33 #define NULL_USE_OPERAND_P              NULL
34 #define NULL_DEF_OPERAND_P              NULL
35
36 /* This represents the DEF operands of a stmt.  */
37 struct def_optype_d
38 {
39   struct def_optype_d *next;
40   tree *def_ptr;
41 };
42 typedef struct def_optype_d *def_optype_p;
43
44 /* This represents the USE operands of a stmt.  */
45 struct use_optype_d 
46 {
47   struct use_optype_d *next;
48   struct ssa_use_operand_d use_ptr;
49 };
50 typedef struct use_optype_d *use_optype_p;
51
52 typedef struct vuse_element_d
53 {
54   tree use_var;
55   struct ssa_use_operand_d use_ptr;
56 } vuse_element_t;
57
58 typedef struct vuse_vec_d
59 {
60   unsigned int num_vuse;
61   vuse_element_t uses[1];
62 } vuse_vec_t;
63 typedef struct vuse_vec_d *vuse_vec_p;
64
65 #define VUSE_VECT_NUM_ELEM(V)           (V).num_vuse
66 #define VUSE_VECT_ELEMENT_NC(V,X)       (V).uses[(X)]
67 #define VUSE_ELEMENT_PTR_NC(V,X)        (&(VUSE_VECT_ELEMENT_NC ((V),(X)).use_ptr))
68 #define VUSE_ELEMENT_VAR_NC(V,X)        (VUSE_VECT_ELEMENT_NC ((V),(X)).use_var)
69
70 #ifdef ENABLE_CHECKING
71 #define VUSE_VECT_ELEMENT(V,X)                                          \
72     (gcc_assert (((unsigned int) (X)) < VUSE_VECT_NUM_ELEM (V)),        \
73      VUSE_VECT_ELEMENT_NC (V,X))
74
75 #define VUSE_ELEMENT_PTR(V,X)                                           \
76     (gcc_assert (((unsigned int) (X)) < VUSE_VECT_NUM_ELEM (V)),        \
77      VUSE_ELEMENT_PTR_NC (V, X))
78
79 #define SET_VUSE_VECT_ELEMENT(V,X,N)                                    \
80     (gcc_assert (((unsigned int) (X)) < VUSE_VECT_NUM_ELEM (V)),        \
81      VUSE_VECT_ELEMENT_NC (V,X) = (N))
82
83 #define SET_VUSE_ELEMENT_VAR(V,X,N)                                     \
84     (gcc_assert (((unsigned int) (X)) < VUSE_VECT_NUM_ELEM (V)),        \
85      VUSE_VECT_ELEMENT_NC ((V),(X)).use_var = (N))
86
87 #define SET_VUSE_ELEMENT_PTR(V,X,N)                                     \
88     (gcc_assert (((unsigned int) (X)) < VUSE_VECT_NUM_ELEM (V)),        \
89      VUSE_ELEMENT_PTR_NC (V, X) = (N))
90 #else
91 #define VUSE_VECT_ELEMENT(V,X) VUSE_VECT_ELEMENT_NC(V,X)
92 #define VUSE_ELEMENT_PTR(V,X) VUSE_ELEMENT_PTR_NC(V,X)
93 #define SET_VUSE_VECT_ELEMENT(V,X,N) VUSE_VECT_ELEMENT_NC(V,X) = (N)
94 #define SET_VUSE_ELEMENT_PTR(V,X,N) VUSE_ELEMENT_PTR_NC(V,X) = (N)
95 #define SET_VUSE_ELEMENT_VAR(V,X,N) VUSE_VECT_ELEMENT_NC ((V),(X)).use_var = (N)
96 #endif
97
98 #define VUSE_ELEMENT_VAR(V,X)   (VUSE_VECT_ELEMENT ((V),(X)).use_var)
99
100 /* This represents the virtual ops of a stmt.  */
101 struct voptype_d
102 {
103   struct voptype_d *next;
104   tree def_var;
105   vuse_vec_t usev;
106 };
107 typedef struct voptype_d *voptype_p;
108
109 /* This structure represents a variable sized buffer which is allocated by the
110    operand memory manager.  Operands are suballocated out of this block.  The
111    MEM array varies in size.  */
112    
113 struct ssa_operand_memory_d GTY((chain_next("%h.next")))
114 {
115   struct ssa_operand_memory_d *next;
116   char mem[1];
117 };
118
119 /* Number of different size free buckets for virtual operands.  */
120 #define NUM_VOP_FREE_BUCKETS            29
121
122 /* Per-function operand caches.  */
123 struct ssa_operands GTY(()) {
124    struct ssa_operand_memory_d *operand_memory;
125    unsigned operand_memory_index;
126    /* Current size of the operand memory buffer.  */
127    unsigned int ssa_operand_mem_size;
128
129    bool ops_active;
130
131    struct def_optype_d * GTY ((skip (""))) free_defs;
132    struct use_optype_d * GTY ((skip (""))) free_uses;
133    struct voptype_d * GTY ((skip (""))) vop_free_buckets[NUM_VOP_FREE_BUCKETS];
134    VEC(tree,heap) * GTY ((skip (""))) mpt_table;
135 };
136
137 /* This represents the operand cache for a stmt.  */
138 struct stmt_operands_d
139 {
140   /* Statement operands.  */
141   struct def_optype_d * def_ops;
142   struct use_optype_d * use_ops;
143                                                                               
144   /* Virtual operands (VDEF, VUSE).  */
145   struct voptype_d * vdef_ops;
146   struct voptype_d * vuse_ops;
147
148   /* Sets of memory symbols loaded and stored.  */
149   bitmap stores;
150   bitmap loads;
151 };
152                                                                               
153 typedef struct stmt_operands_d *stmt_operands_p;
154                                                                               
155 #define USE_FROM_PTR(PTR)       get_use_from_ptr (PTR)
156 #define DEF_FROM_PTR(PTR)       get_def_from_ptr (PTR)
157 #define SET_USE(USE, V)         set_ssa_use_from_ptr (USE, V)
158 #define SET_DEF(DEF, V)         ((*(DEF)) = (V))
159
160 #define USE_STMT(USE)           (USE)->stmt
161
162 #define DEF_OPS(STMT)           (stmt_ann (STMT)->operands.def_ops)
163 #define USE_OPS(STMT)           (stmt_ann (STMT)->operands.use_ops)
164 #define VUSE_OPS(STMT)          (stmt_ann (STMT)->operands.vuse_ops)
165 #define VDEF_OPS(STMT)          (stmt_ann (STMT)->operands.vdef_ops)
166
167 #define LOADED_SYMS(STMT)       (stmt_ann (STMT)->operands.loads)
168 #define STORED_SYMS(STMT)       (stmt_ann (STMT)->operands.stores)
169
170 #define USE_OP_PTR(OP)          (&((OP)->use_ptr))
171 #define USE_OP(OP)              (USE_FROM_PTR (USE_OP_PTR (OP)))
172
173 #define DEF_OP_PTR(OP)          ((OP)->def_ptr)
174 #define DEF_OP(OP)              (DEF_FROM_PTR (DEF_OP_PTR (OP)))
175
176 #define VUSE_OP_PTR(OP,X)       VUSE_ELEMENT_PTR ((OP)->usev, (X)) 
177 #define VUSE_OP(OP,X)           VUSE_ELEMENT_VAR ((OP)->usev, (X))
178 #define SET_VUSE_OP(OP,X,N)     SET_VUSE_ELEMENT_VAR ((OP)->usev, (X), (N))
179 #define VUSE_NUM(OP)            VUSE_VECT_NUM_ELEM ((OP)->usev)
180 #define VUSE_VECT(OP)           &((OP)->usev)
181
182 #define VDEF_RESULT_PTR(OP)     (&((OP)->def_var))
183 #define VDEF_RESULT(OP)         ((OP)->def_var)
184 #define VDEF_OP_PTR(OP,X)       VUSE_OP_PTR (OP, X)
185 #define VDEF_OP(OP,X)           VUSE_OP (OP, X)
186 #define SET_VDEF_OP(OP,X,N)     SET_VUSE_OP (OP, X, N)
187 #define VDEF_NUM(OP)            VUSE_VECT_NUM_ELEM ((OP)->usev)
188 #define VDEF_VECT(OP)           &((OP)->usev)
189
190 #define PHI_RESULT_PTR(PHI)     get_phi_result_ptr (PHI)
191 #define PHI_RESULT(PHI)         DEF_FROM_PTR (PHI_RESULT_PTR (PHI))
192 #define SET_PHI_RESULT(PHI, V)  SET_DEF (PHI_RESULT_PTR (PHI), (V))
193
194 #define PHI_ARG_DEF_PTR(PHI, I) get_phi_arg_def_ptr ((PHI), (I))
195 #define PHI_ARG_DEF(PHI, I)     USE_FROM_PTR (PHI_ARG_DEF_PTR ((PHI), (I)))
196 #define SET_PHI_ARG_DEF(PHI, I, V)                                      \
197                                 SET_USE (PHI_ARG_DEF_PTR ((PHI), (I)), (V))
198 #define PHI_ARG_DEF_FROM_EDGE(PHI, E)                                   \
199                                 PHI_ARG_DEF ((PHI), (E)->dest_idx)
200 #define PHI_ARG_DEF_PTR_FROM_EDGE(PHI, E)                               \
201                                 PHI_ARG_DEF_PTR ((PHI), (E)->dest_idx)
202 #define PHI_ARG_INDEX_FROM_USE(USE)   phi_arg_index_from_use (USE)
203
204
205 extern void init_ssa_operands (void);
206 extern void fini_ssa_operands (void);
207 extern void free_ssa_operands (stmt_operands_p);
208 extern void update_stmt_operands (tree);
209 extern bool verify_imm_links (FILE *f, tree var);
210
211 extern void copy_virtual_operands (tree, tree);
212 extern void create_ssa_artificial_load_stmt (tree, tree);
213
214 extern void dump_immediate_uses (FILE *file);
215 extern void dump_immediate_uses_for (FILE *file, tree var);
216 extern void debug_immediate_uses (void);
217 extern void debug_immediate_uses_for (tree var);
218 extern void dump_decl_set (FILE *, bitmap);
219 extern void debug_decl_set (bitmap);
220
221 extern bool ssa_operands_active (void);
222
223 extern void add_to_addressable_set (tree, bitmap *);
224 extern void push_stmt_changes (tree *);
225 extern void pop_stmt_changes (tree *);
226 extern void discard_stmt_changes (tree *);
227
228 enum ssa_op_iter_type {
229   ssa_op_iter_none = 0,
230   ssa_op_iter_tree,
231   ssa_op_iter_use,
232   ssa_op_iter_def,
233   ssa_op_iter_vdef
234 };
235
236 /* This structure is used in the operand iterator loops.  It contains the 
237    items required to determine which operand is retrieved next.  During
238    optimization, this structure is scalarized, and any unused fields are 
239    optimized away, resulting in little overhead.  */
240
241 typedef struct ssa_operand_iterator_d
242 {
243   def_optype_p defs;
244   use_optype_p uses;
245   voptype_p vuses;
246   voptype_p vdefs;
247   voptype_p mayuses;
248   enum ssa_op_iter_type iter_type;
249   int phi_i;
250   int num_phi;
251   tree phi_stmt;
252   bool done;
253   unsigned int vuse_index;
254   unsigned int mayuse_index;
255 } ssa_op_iter;
256
257 /* These flags are used to determine which operands are returned during 
258    execution of the loop.  */
259 #define SSA_OP_USE              0x01    /* Real USE operands.  */
260 #define SSA_OP_DEF              0x02    /* Real DEF operands.  */
261 #define SSA_OP_VUSE             0x04    /* VUSE operands.  */
262 #define SSA_OP_VMAYUSE          0x08    /* USE portion of VDEFS.  */
263 #define SSA_OP_VDEF             0x10    /* DEF portion of VDEFS.  */
264
265 /* These are commonly grouped operand flags.  */
266 #define SSA_OP_VIRTUAL_USES     (SSA_OP_VUSE | SSA_OP_VMAYUSE)
267 #define SSA_OP_VIRTUAL_DEFS     (SSA_OP_VDEF)
268 #define SSA_OP_ALL_VIRTUALS     (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
269 #define SSA_OP_ALL_USES         (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
270 #define SSA_OP_ALL_DEFS         (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
271 #define SSA_OP_ALL_OPERANDS     (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
272
273 /* This macro executes a loop over the operands of STMT specified in FLAG, 
274    returning each operand as a 'tree' in the variable TREEVAR.  ITER is an
275    ssa_op_iter structure used to control the loop.  */
276 #define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS)   \
277   for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS);      \
278        !op_iter_done (&(ITER));                                 \
279        TREEVAR = op_iter_next_tree (&(ITER)))
280
281 /* This macro executes a loop over the operands of STMT specified in FLAG, 
282    returning each operand as a 'use_operand_p' in the variable USEVAR.  
283    ITER is an ssa_op_iter structure used to control the loop.  */
284 #define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS)     \
285   for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS);        \
286        !op_iter_done (&(ITER));                                 \
287        USEVAR = op_iter_next_use (&(ITER)))
288
289 /* This macro executes a loop over the operands of STMT specified in FLAG, 
290    returning each operand as a 'def_operand_p' in the variable DEFVAR.  
291    ITER is an ssa_op_iter structure used to control the loop.  */
292 #define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS)     \
293   for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS);        \
294        !op_iter_done (&(ITER));                                 \
295        DEFVAR = op_iter_next_def (&(ITER)))
296
297 /* This macro executes a loop over the VDEF operands of STMT.  The def
298    and use vector for each VDEF is returned in DEFVAR and USEVECT. 
299    ITER is an ssa_op_iter structure used to control the loop.  */
300 #define FOR_EACH_SSA_VDEF_OPERAND(DEFVAR, USEVECT, STMT, ITER)  \
301   for (op_iter_init_vdef (&(ITER), STMT, &(USEVECT), &(DEFVAR));        \
302        !op_iter_done (&(ITER));                                 \
303        op_iter_next_vdef (&(USEVECT), &(DEFVAR), &(ITER)))
304
305 /* This macro will execute a loop over all the arguments of a PHI which
306    match FLAGS.   A use_operand_p is always returned via USEVAR.  FLAGS
307    can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES.  */
308 #define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS)             \
309   for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS);   \
310        !op_iter_done (&(ITER));                                 \
311        (USEVAR) = op_iter_next_use (&(ITER)))
312
313
314 /* This macro will execute a loop over a stmt, regardless of whether it is
315    a real stmt or a PHI node, looking at the USE nodes matching FLAGS.  */
316 #define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS)     \
317   for ((USEVAR) = (TREE_CODE (STMT) == PHI_NODE                 \
318                    ? op_iter_init_phiuse (&(ITER), STMT, FLAGS) \
319                    : op_iter_init_use (&(ITER), STMT, FLAGS));  \
320        !op_iter_done (&(ITER));                                 \
321        (USEVAR) = op_iter_next_use (&(ITER)))
322
323 /* This macro will execute a loop over a stmt, regardless of whether it is
324    a real stmt or a PHI node, looking at the DEF nodes matching FLAGS.  */
325 #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS)     \
326   for ((DEFVAR) = (TREE_CODE (STMT) == PHI_NODE                 \
327                    ? op_iter_init_phidef (&(ITER), STMT, FLAGS) \
328                    : op_iter_init_def (&(ITER), STMT, FLAGS));  \
329        !op_iter_done (&(ITER));                                 \
330        (DEFVAR) = op_iter_next_def (&(ITER)))
331   
332 /* This macro returns an operand in STMT as a tree if it is the ONLY
333    operand matching FLAGS.  If there are 0 or more than 1 operand matching
334    FLAGS, then NULL_TREE is returned.  */
335 #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS)                    \
336   single_ssa_tree_operand (STMT, FLAGS)
337                                                                                 
338 /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
339    operand matching FLAGS.  If there are 0 or more than 1 operand matching
340    FLAGS, then NULL_USE_OPERAND_P is returned.  */
341 #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS)                     \
342   single_ssa_use_operand (STMT, FLAGS)
343                                                                                 
344 /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
345    operand matching FLAGS.  If there are 0 or more than 1 operand matching
346    FLAGS, then NULL_DEF_OPERAND_P is returned.  */
347 #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS)                     \
348   single_ssa_def_operand (STMT, FLAGS)
349
350 /* This macro returns TRUE if there are no operands matching FLAGS in STMT.  */
351 #define ZERO_SSA_OPERANDS(STMT, FLAGS)  zero_ssa_operands (STMT, FLAGS)
352
353 /* This macro counts the number of operands in STMT matching FLAGS.  */
354 #define NUM_SSA_OPERANDS(STMT, FLAGS)   num_ssa_operands (STMT, FLAGS)
355
356 #endif  /* GCC_TREE_SSA_OPERANDS_H  */