libcore: Check, that a pointer to a char array is passed to LC_OPT_ENT_STR().
[libfirm] / ir / be / bessadestr.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Performs SSA-Destruction.
23  * @author      Daniel Grund
24  * @date        25.05.2005
25  */
26 #include "config.h"
27
28 #include "bessadestr.h"
29
30 #include "debug.h"
31 #include "set.h"
32 #include "pmap.h"
33 #include "irnode_t.h"
34 #include "ircons_t.h"
35 #include "iredges_t.h"
36 #include "irgwalk.h"
37 #include "irgmod.h"
38 #include "irdump.h"
39 #include "irprintf.h"
40
41 #include "be_t.h"
42 #include "beutil.h"
43 #include "bechordal_t.h"
44 #include "bearch.h"
45 #include "belive_t.h"
46 #include "benode.h"
47 #include "besched.h"
48 #include "statev_t.h"
49 #include "beirg.h"
50 #include "beintlive_t.h"
51 #include "bespillutil.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 static void clear_link(ir_node *irn, void *data)
56 {
57         (void) data;
58         set_irn_link(irn, NULL);
59 }
60
61 /**
62  * For each block build a linked list of phis that
63  *  - are in that block
64  *  - have the current register class
65  * The list is rooted at get_irn_link(BB).
66  */
67 static void collect_phis_walker(ir_node *irn, void *data)
68 {
69         be_chordal_env_t *env = (be_chordal_env_t*)data;
70         if (is_Phi(irn) && arch_irn_consider_in_reg_alloc(env->cls, irn)) {
71                 ir_node *bl = get_nodes_block(irn);
72                 set_irn_link(irn, get_irn_link(bl));
73                 set_irn_link(bl, irn);
74         }
75 }
76
77 /**
78  * This struct represents a Proj for a Perm.
79  * It records the argument in the Perm and the corresponding Proj of the
80  * Perm.
81  */
82 typedef struct {
83         ir_node *arg;  /**< The phi argument to make the Proj for. */
84         int pos;       /**< The proj number the Proj will get.
85                                                                          This also denotes the position of @p arg
86                                                                          in the in array of the Perm. */
87         ir_node *proj; /**< The proj created for @p arg. */
88 } perm_proj_t;
89
90 static int cmp_perm_proj(const void *a, const void *b, size_t n)
91 {
92         const perm_proj_t *p = (const perm_proj_t*)a;
93         const perm_proj_t *q = (const perm_proj_t*)b;
94         (void) n;
95
96         return !(p->arg == q->arg);
97 }
98
99 /**
100  * Insert Perms in all predecessors of a block containing a phi
101  */
102 static void insert_all_perms_walker(ir_node *bl, void *data)
103 {
104         be_chordal_env_t *const chordal_env = (be_chordal_env_t*)data;
105         be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
106         int i, n;
107
108         assert(is_Block(bl));
109
110         /* If the link flag is NULL, this block has no phis. */
111         if (!get_irn_link(bl))
112                 return;
113
114         /* Look at all predecessors of the phi block */
115         for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
116                 ir_node *phi, *perm, **in;
117                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
118                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
119                 int n_projs      = 0;
120
121                 /*
122                  * Note that all phis in the list are in the same
123                  * register class by construction.
124                  */
125                 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
126                      phi = (ir_node*)get_irn_link(phi)) {
127                         ir_node                   *arg = get_irn_n(phi, i);
128                         unsigned                   hash;
129                         perm_proj_t                templ;
130
131                         hash = hash_irn(arg);
132                         templ.arg  = arg;
133                         perm_proj_t *const pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
134
135                         /*
136                          * If a proj_perm_t entry has not been made in the argument set,
137                          * create one. The only restriction is, that the phi argument
138                          * may not be live in at the current block, since this argument
139                          * interferes with the phi and must thus not be member of a
140                          * Perm. A copy will be inserted for this argument later on.
141                          */
142                         if (!pp && !be_is_live_in(lv, bl, arg)) {
143                                 templ.pos = n_projs++;
144                                 (void)set_insert(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
145                         }
146                 }
147
148
149                 if (n_projs) {
150                         /*
151                          * Create a new Perm with the arguments just collected
152                          * above in the arg_set and insert it into the schedule.
153                          */
154                         in = XMALLOCN(ir_node*, n_projs);
155                         foreach_set(arg_set, perm_proj_t, pp) {
156                                 in[pp->pos] = pp->arg;
157                         }
158
159                         perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
160                         stat_ev_int("phi_perm", n_projs);
161
162                         ir_node *const schedpoint = be_get_end_of_block_insertion_point(pred_bl);
163                         sched_add_before(schedpoint, perm);
164
165                         /*
166                          * Make the Projs for the Perm and insert into schedule.
167                          * Register allocation is copied from the former phi
168                          * arguments to the projs (new phi arguments).
169                          */
170                         foreach_set(arg_set, perm_proj_t, pp) {
171                                 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
172                                 pp->proj = proj;
173                                 assert(arch_get_irn_register(pp->arg));
174                                 arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
175                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
176                         }
177
178                         /*
179                          * Set the phi nodes to their new arguments: The Projs of the Perm
180                          */
181                         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
182                              phi = (ir_node*)get_irn_link(phi)) {
183                                 perm_proj_t templ;
184
185                                 templ.arg = get_irn_n(phi, i);
186                                 perm_proj_t *const pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
187
188                                 /* If not found, it was an interfering argument */
189                                 if (pp) {
190                                         set_irn_n(phi, i, pp->proj);
191                                         be_liveness_introduce(lv, pp->proj);
192                                 }
193                         }
194
195                         /* update the liveness of the Perm's operands. It might be changed. */
196                         {
197                                 int i;
198                                 for (i = 0; i < n_projs; ++i)
199                                         be_liveness_update(lv, in[i]);
200                         }
201                         free(in);
202                 }
203
204                 del_set(arg_set);
205         }
206 }
207
208 #define is_pinned(irn) (get_irn_link(irn))
209 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
210 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
211
212 /**
213  * Adjusts the register allocation for the (new) phi-operands
214  * and insert duplicates iff necessary.
215  */
216 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data)
217 {
218         be_chordal_env_t *chordal_env = (be_chordal_env_t*)data;
219         be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
220         ir_node *phi;
221
222         /* Consider all phis of this block */
223         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
224              phi = (ir_node*)get_irn_link(phi)) {
225                 ir_node                     *phi_block = get_nodes_block(phi);
226                 const arch_register_t       *phi_reg   = arch_get_irn_register(phi);
227                 int                          max;
228                 int                          i;
229
230                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
231
232                 /* process all arguments of the phi */
233                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
234                         ir_node                   *arg = get_irn_n(phi, i);
235                         const arch_register_t     *arg_reg;
236                         ir_node                   *arg_block;
237
238                         arg_block = get_Block_cfgpred_block(phi_block, i);
239                         arg_reg   = arch_get_irn_register(arg);
240
241                         assert(arg_reg && "Register must be set while placing perms");
242
243                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
244
245                         if (phi_reg == arg_reg
246                                         || (arg_reg->type & arch_register_type_virtual)) {
247                                 /* Phi and arg have the same register, so pin and continue */
248                                 pin_irn(arg, phi_block);
249                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
250                                 continue;
251                         }
252
253                         if (be_values_interfere(lv, phi, arg)) {
254                                 /*
255                                         Insert a duplicate in arguments block,
256                                         make it the new phi arg,
257                                         set its register,
258                                         insert it into schedule,
259                                         pin it
260                                 */
261                                 ir_node *dupl = be_new_Copy(arg_block, arg);
262
263                                 set_irn_n(phi, i, dupl);
264                                 arch_set_irn_register(dupl, phi_reg);
265                                 ir_node *const schedpoint = be_get_end_of_block_insertion_point(arg_block);
266                                 sched_add_before(schedpoint, dupl);
267                                 pin_irn(dupl, phi_block);
268                                 be_liveness_introduce(lv, dupl);
269                                 be_liveness_update(lv, arg);
270                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
271                                 continue; /* with next argument */
272                         }
273
274                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
275                         assert(is_Proj(arg));
276                         /*
277                                 First check if there is an other phi
278                                 - in the same block
279                                 - having arg at the current pos in its arg-list
280                                 - having the same color as arg
281
282                                 If found, then pin the arg (for that phi)
283                         */
284                         if (! is_pinned(arg)) {
285                                 ir_node *other_phi;
286
287                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
288
289                                 for (other_phi = (ir_node*)get_irn_link(phi_block);
290                                      other_phi != NULL;
291                                      other_phi = (ir_node*)get_irn_link(other_phi)) {
292
293                                         assert(is_Phi(other_phi)                               &&
294                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
295                                                 "link fields are screwed up");
296
297                                         if (get_irn_n(other_phi, i) == arg && arch_get_irn_register(other_phi) == arg_reg) {
298                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, arch_get_irn_register(other_phi)->name));
299                                                 pin_irn(arg, phi_block);
300                                                 break;
301                                         }
302                                 }
303                         }
304
305                         if (is_pinned(arg)) {
306                                 /*
307                                         Insert a duplicate of the original value in arguments block,
308                                         make it the new phi arg,
309                                         set its register,
310                                         insert it into schedule,
311                                         pin it
312                                 */
313                                 ir_node *perm = get_Proj_pred(arg);
314                                 ir_node *dupl = be_new_Copy(arg_block, arg);
315
316                                 set_irn_n(phi, i, dupl);
317                                 arch_set_irn_register(dupl, phi_reg);
318                                 sched_add_after(perm, dupl);
319                                 pin_irn(dupl, phi_block);
320                                 be_liveness_introduce(lv, dupl);
321                                 be_liveness_update(lv, arg);
322                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
323                         } else {
324                                 /*
325                                         No other phi has the same color (else arg would have been pinned),
326                                         so just set the register and pin
327                                 */
328                                 arch_set_irn_register(arg, phi_reg);
329                                 pin_irn(arg, phi_block);
330                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
331                         }
332                 }
333         }
334 }
335
336 void be_ssa_destruction(be_chordal_env_t *chordal_env)
337 {
338         ir_graph *irg = chordal_env->irg;
339
340         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
341
342         be_invalidate_live_sets(irg);
343
344         /* create a map for fast lookup of perms: block --> perm */
345         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
346
347         DBG((dbg, LEVEL_1, "Placing perms...\n"));
348         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
349
350         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
351                 dump_ir_graph(irg, "ssa_destr_perms_placed");
352
353         be_assure_live_chk(irg);
354
355         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
356         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
357
358         /* unfortunately updating doesn't work yet. */
359         be_invalidate_live_chk(irg);
360
361         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
362                 dump_ir_graph(irg, "ssa_destr_regs_set");
363 }
364
365 static void ssa_destruction_check_walker(ir_node *bl, void *data)
366 {
367         ir_node *phi;
368         int i, max;
369         (void)data;
370
371         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
372              phi = (ir_node*)get_irn_link(phi)) {
373                 const arch_register_t *phi_reg, *arg_reg;
374
375                 phi_reg = arch_get_irn_register(phi);
376                 /* iterate over all args of phi */
377                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
378                         ir_node                   *arg = get_irn_n(phi, i);
379                         const arch_register_req_t *req = arch_get_irn_register_req(arg);
380                         if (arch_register_req_is(req, ignore))
381                                 continue;
382
383                         arg_reg = arch_get_irn_register(arg);
384
385                         if (phi_reg != arg_reg) {
386                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
387                                 assert(0);
388                         }
389
390                         if (! is_pinned(arg)) {
391                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
392                                 assert(0);
393                         }
394                 }
395         }
396 }
397
398 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
399 {
400         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);
401 }