1/* SSA operand management for trees.
2   Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING.  If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA.  */
20
21#ifndef GCC_TREE_SSA_OPERANDS_H
22#define GCC_TREE_SSA_OPERANDS_H
23
24/* Interface to SSA operands.  */
25
26
27/* This represents a pointer to a DEF operand.  */
28typedef tree *def_operand_p;
29
30/* This represents a pointer to a USE operand.  */
31typedef ssa_use_operand_t *use_operand_p;
32
33/* NULL operand types.  */
34#define NULL_USE_OPERAND_P 		NULL
35#define NULL_DEF_OPERAND_P 		NULL
36
37/* This represents the DEF operands of a stmt.  */
38struct def_optype_d
39{
40  struct def_optype_d *next;
41  tree *def_ptr;
42};
43typedef struct def_optype_d *def_optype_p;
44
45/* This represents the USE operands of a stmt.  */
46struct use_optype_d
47{
48  struct use_optype_d *next;
49  struct ssa_use_operand_d use_ptr;
50};
51typedef struct use_optype_d *use_optype_p;
52
53/* This represents the MAY_DEFS for a stmt.  */
54struct maydef_optype_d
55{
56  struct maydef_optype_d *next;
57  tree def_var;
58  tree use_var;
59  struct ssa_use_operand_d use_ptr;
60};
61typedef struct maydef_optype_d *maydef_optype_p;
62
63/* This represents the VUSEs for a stmt.  */
64struct vuse_optype_d
65{
66  struct vuse_optype_d *next;
67  tree use_var;
68  struct ssa_use_operand_d use_ptr;
69};
70typedef struct vuse_optype_d *vuse_optype_p;
71
72/* This represents the V_MUST_DEFS for a stmt.  */
73struct mustdef_optype_d
74{
75  struct mustdef_optype_d *next;
76  tree def_var;
77  tree kill_var;
78  struct ssa_use_operand_d use_ptr;
79};
80typedef struct mustdef_optype_d *mustdef_optype_p;
81
82
83#define SSA_OPERAND_MEMORY_SIZE		(2048 - sizeof (void *))
84
85struct ssa_operand_memory_d GTY((chain_next("%h.next")))
86{
87  struct ssa_operand_memory_d *next;
88  char mem[SSA_OPERAND_MEMORY_SIZE];
89};
90
91
92/* This represents the operand cache for a stmt.  */
93struct stmt_operands_d
94{
95  /* Statement operands.  */
96  struct def_optype_d * def_ops;
97  struct use_optype_d * use_ops;
98
99  /* Virtual operands (V_MAY_DEF, VUSE, and V_MUST_DEF).  */
100  struct maydef_optype_d * maydef_ops;
101  struct vuse_optype_d * vuse_ops;
102  struct mustdef_optype_d * mustdef_ops;
103};
104
105typedef struct stmt_operands_d *stmt_operands_p;
106
107#define USE_FROM_PTR(PTR)	get_use_from_ptr (PTR)
108#define DEF_FROM_PTR(PTR)	get_def_from_ptr (PTR)
109#define SET_USE(USE, V)		set_ssa_use_from_ptr (USE, V)
110#define SET_DEF(DEF, V)		((*(DEF)) = (V))
111
112#define USE_STMT(USE)		(USE)->stmt
113
114#define DEF_OPS(STMT)		(stmt_ann (STMT)->operands.def_ops)
115#define USE_OPS(STMT)		(stmt_ann (STMT)->operands.use_ops)
116#define VUSE_OPS(STMT)		(stmt_ann (STMT)->operands.vuse_ops)
117#define MAYDEF_OPS(STMT)	(stmt_ann (STMT)->operands.maydef_ops)
118#define MUSTDEF_OPS(STMT)	(stmt_ann (STMT)->operands.mustdef_ops)
119
120#define USE_OP_PTR(OP)		(&((OP)->use_ptr))
121#define USE_OP(OP)		(USE_FROM_PTR (USE_OP_PTR (OP)))
122
123#define DEF_OP_PTR(OP)		((OP)->def_ptr)
124#define DEF_OP(OP)		(DEF_FROM_PTR (DEF_OP_PTR (OP)))
125
126#define VUSE_OP_PTR(OP)		USE_OP_PTR(OP)
127#define VUSE_OP(OP)		((OP)->use_var)
128
129#define MAYDEF_RESULT_PTR(OP)	(&((OP)->def_var))
130#define MAYDEF_RESULT(OP)	((OP)->def_var)
131#define MAYDEF_OP_PTR(OP)	USE_OP_PTR (OP)
132#define MAYDEF_OP(OP)		((OP)->use_var)
133
134#define MUSTDEF_RESULT_PTR(OP)	(&((OP)->def_var))
135#define MUSTDEF_RESULT(OP)	((OP)->def_var)
136#define MUSTDEF_KILL_PTR(OP)	USE_OP_PTR (OP)
137#define MUSTDEF_KILL(OP)	((OP)->kill_var)
138
139#define PHI_RESULT_PTR(PHI)	get_phi_result_ptr (PHI)
140#define PHI_RESULT(PHI)		DEF_FROM_PTR (PHI_RESULT_PTR (PHI))
141#define SET_PHI_RESULT(PHI, V)	SET_DEF (PHI_RESULT_PTR (PHI), (V))
142
143#define PHI_ARG_DEF_PTR(PHI, I)	get_phi_arg_def_ptr ((PHI), (I))
144#define PHI_ARG_DEF(PHI, I)	USE_FROM_PTR (PHI_ARG_DEF_PTR ((PHI), (I)))
145#define SET_PHI_ARG_DEF(PHI, I, V)					\
146				SET_USE (PHI_ARG_DEF_PTR ((PHI), (I)), (V))
147#define PHI_ARG_DEF_FROM_EDGE(PHI, E)					\
148				PHI_ARG_DEF ((PHI), (E)->dest_idx)
149#define PHI_ARG_DEF_PTR_FROM_EDGE(PHI, E)				\
150				PHI_ARG_DEF_PTR ((PHI), (E)->dest_idx)
151#define PHI_ARG_INDEX_FROM_USE(USE)   phi_arg_index_from_use (USE)
152
153
154extern void init_ssa_operands (void);
155extern void fini_ssa_operands (void);
156extern void free_ssa_operands (stmt_operands_p);
157extern void update_stmt_operands (tree);
158extern bool verify_imm_links (FILE *f, tree var);
159
160extern void copy_virtual_operands (tree, tree);
161extern void create_ssa_artficial_load_stmt (tree, tree);
162
163extern void dump_immediate_uses (FILE *file);
164extern void dump_immediate_uses_for (FILE *file, tree var);
165extern void debug_immediate_uses (void);
166extern void debug_immediate_uses_for (tree var);
167
168extern bool ssa_operands_active (void);
169
170extern void add_to_addressable_set (tree, bitmap *);
171
172enum ssa_op_iter_type {
173  ssa_op_iter_none = 0,
174  ssa_op_iter_tree,
175  ssa_op_iter_use,
176  ssa_op_iter_def,
177  ssa_op_iter_maymustdef
178};
179/* This structure is used in the operand iterator loops.  It contains the
180   items required to determine which operand is retrieved next.  During
181   optimization, this structure is scalarized, and any unused fields are
182   optimized away, resulting in little overhead.  */
183
184typedef struct ssa_operand_iterator_d
185{
186  def_optype_p defs;
187  use_optype_p uses;
188  vuse_optype_p vuses;
189  maydef_optype_p maydefs;
190  maydef_optype_p mayuses;
191  mustdef_optype_p mustdefs;
192  mustdef_optype_p mustkills;
193  enum ssa_op_iter_type iter_type;
194  int phi_i;
195  int num_phi;
196  tree phi_stmt;
197  bool done;
198} ssa_op_iter;
199
200/* These flags are used to determine which operands are returned during
201   execution of the loop.  */
202#define SSA_OP_USE		0x01	/* Real USE operands.  */
203#define SSA_OP_DEF		0x02	/* Real DEF operands.  */
204#define SSA_OP_VUSE		0x04	/* VUSE operands.  */
205#define SSA_OP_VMAYUSE		0x08	/* USE portion of V_MAY_DEFS.  */
206#define SSA_OP_VMAYDEF		0x10	/* DEF portion of V_MAY_DEFS.  */
207#define SSA_OP_VMUSTDEF		0x20	/* V_MUST_DEF definitions.  */
208#define SSA_OP_VMUSTKILL     	0x40    /* V_MUST_DEF kills.  */
209
210/* These are commonly grouped operand flags.  */
211#define SSA_OP_VIRTUAL_USES	(SSA_OP_VUSE | SSA_OP_VMAYUSE)
212#define SSA_OP_VIRTUAL_DEFS	(SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF)
213#define SSA_OP_VIRTUAL_KILLS    (SSA_OP_VMUSTKILL)
214#define SSA_OP_ALL_VIRTUALS     (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS \
215				 | SSA_OP_VIRTUAL_DEFS)
216#define SSA_OP_ALL_USES		(SSA_OP_VIRTUAL_USES | SSA_OP_USE)
217#define SSA_OP_ALL_DEFS		(SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
218#define SSA_OP_ALL_KILLS        (SSA_OP_VIRTUAL_KILLS)
219#define SSA_OP_ALL_OPERANDS	(SSA_OP_ALL_USES | SSA_OP_ALL_DEFS	\
220				 | SSA_OP_ALL_KILLS)
221
222/* This macro executes a loop over the operands of STMT specified in FLAG,
223   returning each operand as a 'tree' in the variable TREEVAR.  ITER is an
224   ssa_op_iter structure used to control the loop.  */
225#define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS)	\
226  for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS);	\
227       !op_iter_done (&(ITER));					\
228       TREEVAR = op_iter_next_tree (&(ITER)))
229
230/* This macro executes a loop over the operands of STMT specified in FLAG,
231   returning each operand as a 'use_operand_p' in the variable USEVAR.
232   ITER is an ssa_op_iter structure used to control the loop.  */
233#define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS)	\
234  for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS);	\
235       !op_iter_done (&(ITER));					\
236       USEVAR = op_iter_next_use (&(ITER)))
237
238/* This macro executes a loop over the operands of STMT specified in FLAG,
239   returning each operand as a 'def_operand_p' in the variable DEFVAR.
240   ITER is an ssa_op_iter structure used to control the loop.  */
241#define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS)	\
242  for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS);	\
243       !op_iter_done (&(ITER));					\
244       DEFVAR = op_iter_next_def (&(ITER)))
245
246/* This macro executes a loop over the V_MAY_DEF operands of STMT.  The def
247   and use for each V_MAY_DEF is returned in DEFVAR and USEVAR.
248   ITER is an ssa_op_iter structure used to control the loop.  */
249#define FOR_EACH_SSA_MAYDEF_OPERAND(DEFVAR, USEVAR, STMT, ITER)	\
250  for (op_iter_init_maydef (&(ITER), STMT, &(USEVAR), &(DEFVAR));	\
251       !op_iter_done (&(ITER));					\
252       op_iter_next_maymustdef (&(USEVAR), &(DEFVAR), &(ITER)))
253
254/* This macro executes a loop over the V_MUST_DEF operands of STMT.  The def
255   and kill for each V_MUST_DEF is returned in DEFVAR and KILLVAR.
256   ITER is an ssa_op_iter structure used to control the loop.  */
257#define FOR_EACH_SSA_MUSTDEF_OPERAND(DEFVAR, KILLVAR, STMT, ITER)	\
258  for (op_iter_init_mustdef (&(ITER), STMT, &(KILLVAR), &(DEFVAR));	\
259       !op_iter_done (&(ITER));					\
260       op_iter_next_maymustdef (&(KILLVAR), &(DEFVAR), &(ITER)))
261
262/* This macro executes a loop over the V_{MUST,MAY}_DEF of STMT.  The def
263   and kill for each V_{MUST,MAY}_DEF is returned in DEFVAR and KILLVAR.
264   ITER is an ssa_op_iter structure used to control the loop.  */
265#define FOR_EACH_SSA_MUST_AND_MAY_DEF_OPERAND(DEFVAR, KILLVAR, STMT, ITER)\
266  for (op_iter_init_must_and_may_def (&(ITER), STMT, &(KILLVAR), &(DEFVAR));\
267       !op_iter_done (&(ITER));					\
268       op_iter_next_maymustdef (&(KILLVAR), &(DEFVAR), &(ITER)))
269
270/* This macro will execute a loop over all the arguments of a PHI which
271   match FLAGS.   A use_operand_p is always returned via USEVAR.  FLAGS
272   can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES.  */
273#define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS)		\
274  for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS);	\
275       !op_iter_done (&(ITER));					\
276       (USEVAR) = op_iter_next_use (&(ITER)))
277
278
279/* This macro will execute a loop over a stmt, regardless of whether it is
280   a real stmt or a PHI node, looking at the USE nodes matching FLAGS.  */
281#define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS)	\
282  for ((USEVAR) = (TREE_CODE (STMT) == PHI_NODE 		\
283		   ? op_iter_init_phiuse (&(ITER), STMT, FLAGS)	\
284		   : op_iter_init_use (&(ITER), STMT, FLAGS));	\
285       !op_iter_done (&(ITER));					\
286       (USEVAR) = op_iter_next_use (&(ITER)))
287
288/* This macro will execute a loop over a stmt, regardless of whether it is
289   a real stmt or a PHI node, looking at the DEF nodes matching FLAGS.  */
290#define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS)	\
291  for ((DEFVAR) = (TREE_CODE (STMT) == PHI_NODE 		\
292		   ? op_iter_init_phidef (&(ITER), STMT, FLAGS)	\
293		   : op_iter_init_def (&(ITER), STMT, FLAGS));	\
294       !op_iter_done (&(ITER));					\
295       (DEFVAR) = op_iter_next_def (&(ITER)))
296
297/* This macro returns an operand in STMT as a tree if it is the ONLY
298   operand matching FLAGS.  If there are 0 or more than 1 operand matching
299   FLAGS, then NULL_TREE is returned.  */
300#define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS)			\
301  single_ssa_tree_operand (STMT, FLAGS)
302
303/* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
304   operand matching FLAGS.  If there are 0 or more than 1 operand matching
305   FLAGS, then NULL_USE_OPERAND_P is returned.  */
306#define SINGLE_SSA_USE_OPERAND(STMT, FLAGS)			\
307  single_ssa_use_operand (STMT, FLAGS)
308
309/* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
310   operand matching FLAGS.  If there are 0 or more than 1 operand matching
311   FLAGS, then NULL_DEF_OPERAND_P is returned.  */
312#define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS)			\
313  single_ssa_def_operand (STMT, FLAGS)
314
315/* This macro returns TRUE if there are no operands matching FLAGS in STMT.  */
316#define ZERO_SSA_OPERANDS(STMT, FLAGS) 	zero_ssa_operands (STMT, FLAGS)
317
318/* This macro counts the number of operands in STMT matching FLAGS.  */
319#define NUM_SSA_OPERANDS(STMT, FLAGS)	num_ssa_operands (STMT, FLAGS)
320
321#endif  /* GCC_TREE_SSA_OPERANDS_H  */
322