fix IncSP optimizer, remove some cases of nodes not removed from outedges
[libfirm] / ir / be / ia32 / ia32_optimize.c
1 /*
2  * Copyright (C) 1995-2007 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       Implements several optimizations for IA32.
23  * @author      Christian Wuerdig
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "irnode.h"
31 #include "irprog_t.h"
32 #include "ircons.h"
33 #include "firm_types.h"
34 #include "iredges.h"
35 #include "tv.h"
36 #include "irgmod.h"
37 #include "irgwalk.h"
38 #include "height.h"
39 #include "irbitset.h"
40
41 #include "../be_t.h"
42 #include "../beabi.h"
43 #include "../benode_t.h"
44 #include "../besched_t.h"
45
46 #include "ia32_new_nodes.h"
47 #include "bearch_ia32_t.h"
48 #include "gen_ia32_regalloc_if.h"
49 #include "ia32_transform.h"
50 #include "ia32_dbg_stat.h"
51 #include "ia32_util.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 #define AGGRESSIVE_AM
56
57 typedef enum {
58         IA32_AM_CAND_NONE  = 0,  /**< no addressmode possible with irn inputs */
59         IA32_AM_CAND_LEFT  = 1,  /**< addressmode possible with left input */
60         IA32_AM_CAND_RIGHT = 2,  /**< addressmode possible with right input */
61         IA32_AM_CAND_BOTH  = 3   /**< addressmode possible with both inputs */
62 } ia32_am_cand_t;
63
64 typedef int is_op_func_t(const ir_node *n);
65 typedef ir_node *load_func_t(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *base, ir_node *index, ir_node *mem);
66
67 /**
68  * checks if a node represents the NOREG value
69  */
70 static INLINE int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
71         return irn == cg->noreg_gp || irn == cg->noreg_xmm || irn == cg->noreg_vfp;
72 }
73
74 /********************************************************************************************************
75  *  _____                _           _         ____        _   _           _          _   _
76  * |  __ \              | |         | |       / __ \      | | (_)         (_)        | | (_)
77  * | |__) |__  ___ _ __ | |__   ___ | | ___  | |  | |_ __ | |_ _ _ __ ___  _ ______ _| |_ _  ___  _ __
78  * |  ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | |  | | '_ \| __| | '_ ` _ \| |_  / _` | __| |/ _ \| '_ \
79  * | |  |  __/  __/ |_) | | | | (_) | |  __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
80  * |_|   \___|\___| .__/|_| |_|\___/|_|\___|  \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
81  *                | |                               | |
82  *                |_|                               |_|
83  ********************************************************************************************************/
84
85 /**
86  * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
87  */
88
89 // only optimize up to 48 stores behind IncSPs
90 #define MAXPUSH_OPTIMIZE        48
91
92 /**
93  * Tries to create pushs from IncSP,Store combinations
94  */
95 static void ia32_create_Pushs(ir_node *irn, ia32_code_gen_t *cg) {
96         int i;
97         int offset;
98         ir_node *node;
99         ir_node *stores[MAXPUSH_OPTIMIZE];
100         ir_node *block = get_nodes_block(irn);
101         ir_graph *irg = cg->irg;
102         ir_node *curr_sp;
103         ir_mode *spmode = get_irn_mode(irn);
104
105         memset(stores, 0, sizeof(stores));
106
107         assert(be_is_IncSP(irn));
108
109         offset = be_get_IncSP_offset(irn);
110         if(offset < 4)
111                 return;
112
113         /*
114          * We first walk the schedule after the IncSP node as long as we find
115          * suitable stores that could be transformed to a push.
116          * We save them into the stores array which is sorted by the frame offset/4
117          * attached to the node
118          */
119         for(node = sched_next(irn); !sched_is_end(node); node = sched_next(node)) {
120                 ir_node *mem;
121                 int offset;
122                 int storeslot;
123
124                 // it has to be a store
125                 if(!is_ia32_Store(node))
126                         break;
127
128                 // it has to use our sp value
129                 if(get_irn_n(node, 0) != irn)
130                         continue;
131                 // store has to be attached to NoMem
132                 mem = get_irn_n(node, 3);
133                 if(!is_NoMem(mem)) {
134                         continue;
135                 }
136
137                 if( (get_ia32_am_flavour(node) & ia32_am_IS) != 0)
138                         break;
139
140                 offset = get_ia32_am_offs_int(node);
141
142                 storeslot = offset / 4;
143                 if(storeslot >= MAXPUSH_OPTIMIZE)
144                         continue;
145
146                 // storing into the same slot twice is bad (and shouldn't happen...)
147                 if(stores[storeslot] != NULL)
148                         break;
149
150                 // storing at half-slots is bad
151                 if(offset % 4 != 0)
152                         break;
153
154                 stores[storeslot] = node;
155         }
156
157         curr_sp = get_irn_n(irn, 0);
158
159         // walk the stores in inverse order and create pushs for them
160         i = (offset / 4) - 1;
161         if(i >= MAXPUSH_OPTIMIZE) {
162                 i = MAXPUSH_OPTIMIZE - 1;
163         }
164
165         for( ; i >= 0; --i) {
166                 const arch_register_t *spreg;
167                 ir_node *push;
168                 ir_node *val, *mem, *mem_proj;
169                 ir_node *store = stores[i];
170                 ir_node *noreg = ia32_new_NoReg_gp(cg);
171
172                 if(store == NULL || is_Bad(store))
173                         break;
174
175                 val = get_irn_n(store, 2);
176                 mem = get_irn_n(store, 3);
177                 spreg = arch_get_irn_register(cg->arch_env, curr_sp);
178
179                 // create a push
180                 push = new_rd_ia32_Push(NULL, irg, block, noreg, noreg, val, curr_sp, mem);
181
182                 set_ia32_am_support(push, ia32_am_Source, ia32_am_unary);
183                 copy_ia32_Immop_attr(push, store);
184
185                 sched_add_before(irn, push);
186
187                 // create stackpointer proj
188                 curr_sp = new_r_Proj(irg, block, push, spmode, pn_ia32_Push_stack);
189                 arch_set_irn_register(cg->arch_env, curr_sp, spreg);
190 #ifdef SCHEDULE_PROJS
191                 sched_add_before(irn, curr_sp);
192 #endif
193                 // create memory proj
194                 mem_proj = new_r_Proj(irg, block, push, mode_M, pn_ia32_Push_M);
195
196                 // use the memproj now
197                 exchange(store, mem_proj);
198
199                 // we can remove the store now
200                 sched_remove(store);
201
202                 offset -= 4;
203         }
204
205         be_set_IncSP_offset(irn, offset);
206
207         // can we remove the IncSP now?
208         if(offset == 0) {
209                 const ir_edge_t *edge, *next;
210
211                 foreach_out_edge_safe(irn, edge, next) {
212                         ir_node *arg = get_edge_src_irn(edge);
213                         int pos = get_edge_src_pos(edge);
214
215                         set_irn_n(arg, pos, curr_sp);
216                 }
217
218                 set_irn_n(irn, 0, new_Bad());
219                 sched_remove(irn);
220         } else {
221                 set_irn_n(irn, 0, curr_sp);
222         }
223 }
224
225 /**
226  * Tries to optimize two following IncSP.
227  */
228 static void ia32_optimize_IncSP(ir_node *node)
229 {
230         int      pred_offs;
231         int      curr_offs;
232         int      offs;
233         ir_node *pred = be_get_IncSP_pred(node);
234         ir_node *predpred;
235
236         if(!be_is_IncSP(pred))
237                 return;
238
239         if(get_irn_n_edges(pred) > 1)
240                 return;
241
242         pred_offs = be_get_IncSP_offset(pred);
243         curr_offs = be_get_IncSP_offset(node);
244
245         if(pred_offs == BE_STACK_FRAME_SIZE_EXPAND) {
246                 if(curr_offs != BE_STACK_FRAME_SIZE_SHRINK) {
247                         return;
248                 }
249                 offs = 0;
250         } else if(pred_offs == BE_STACK_FRAME_SIZE_SHRINK) {
251                 if(curr_offs != BE_STACK_FRAME_SIZE_EXPAND) {
252                         return;
253                 }
254                 offs = 0;
255         } else if(curr_offs == BE_STACK_FRAME_SIZE_EXPAND
256                         || curr_offs == BE_STACK_FRAME_SIZE_SHRINK) {
257                 return;
258         } else {
259                 offs = curr_offs + pred_offs;
260         }
261
262         be_set_IncSP_offset(node, offs);
263
264         /* rewire dependency edges */
265         predpred = be_get_IncSP_pred(pred);
266         edges_reroute_kind(pred, predpred, EDGE_KIND_DEP, current_ir_graph);
267
268         /* Omit the IncSP */
269         be_set_IncSP_pred(node, predpred);
270         sched_remove(pred);
271         be_kill_node(pred);
272 }
273
274 /**
275  * Performs Peephole Optimizations.
276  */
277 static void ia32_peephole_optimize_node(ir_node *node, void *env) {
278         ia32_code_gen_t *cg = env;
279
280         if (be_is_IncSP(node)) {
281                 ia32_optimize_IncSP(node);
282
283                 if (cg->opt & IA32_OPT_PUSHARGS)
284                         ia32_create_Pushs(node, cg);
285         }
286 }
287
288 void ia32_peephole_optimization(ir_graph *irg, ia32_code_gen_t *cg) {
289         irg_walk_graph(irg, ia32_peephole_optimize_node, NULL, cg);
290 }
291
292 /******************************************************************
293  *              _     _                   __  __           _
294  *     /\      | |   | |                 |  \/  |         | |
295  *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
296  *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
297  *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
298  * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
299  *
300  ******************************************************************/
301
302 typedef struct {
303         ia32_code_gen_t *cg;
304         heights_t       *h;
305 } ia32_am_opt_env_t;
306
307 static int node_is_ia32_comm(const ir_node *irn) {
308         return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
309 }
310
311 static int ia32_get_irn_n_edges(const ir_node *irn) {
312         const ir_edge_t *edge;
313         int cnt = 0;
314
315         foreach_out_edge(irn, edge) {
316                 cnt++;
317         }
318
319         return cnt;
320 }
321
322 /**
323  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
324  *
325  * @param pred       The node to be checked
326  * @param is_op_func The check-function
327  * @return 1 if conditions are fulfilled, 0 otherwise
328  */
329 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
330         return is_op_func(pred);
331 }
332
333 /**
334  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
335  * and if the predecessor is in block bl.
336  *
337  * @param bl         The block
338  * @param pred       The node to be checked
339  * @param is_op_func The check-function
340  * @return 1 if conditions are fulfilled, 0 otherwise
341  */
342 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
343         int (*is_op_func)(const ir_node *n))
344 {
345         if (is_Proj(pred)) {
346                 pred = get_Proj_pred(pred);
347                 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
348                         return 1;
349                 }
350         }
351
352         return 0;
353 }
354
355 /**
356  * Checks if irn is a candidate for address calculation. We avoid transforming
357  * adds to leas if they have a load as pred, because then we can use AM mode
358  * for the add later.
359  *
360  * - none of the operand must be a Load  within the same block OR
361  * - all Loads must have more than one user                    OR
362  *
363  * @param block   The block the Loads must/mustnot be in
364  * @param irn     The irn to check
365  * return 1 if irn is a candidate, 0 otherwise
366  */
367 static int is_addr_candidate(const ir_node *irn)
368 {
369 #ifndef AGGRESSIVE_AM
370         const ir_node *block = get_nodes_block(irn);
371         ir_node *left, *right;
372         int      n;
373
374         left  = get_irn_n(irn, 2);
375         right = get_irn_n(irn, 3);
376
377         if (pred_is_specific_nodeblock(block, left, is_ia32_Ld)) {
378                 n         = ia32_get_irn_n_edges(left);
379                 /* load with only one user: don't create LEA */
380                 if(n == 1)
381                         return 0;
382         }
383
384         if (pred_is_specific_nodeblock(block, right, is_ia32_Ld)) {
385                 n         = ia32_get_irn_n_edges(right);
386                 if(n == 1)
387                         return 0;
388         }
389 #endif
390
391         (void) irn;
392         return 1;
393 }
394
395 /**
396  * Checks if irn is a candidate for address mode.
397  *
398  * address mode (AM):
399  * - at least one operand has to be a Load within the same block AND
400  * - the load must not have other users than the irn             AND
401  * - the irn must not have a frame entity set
402  *
403  * @param cg          The ia32 code generator
404  * @param h           The height information of the irg
405  * @param block       The block the Loads must/mustnot be in
406  * @param irn         The irn to check
407  * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
408  */
409 static ia32_am_cand_t is_am_candidate(heights_t *h, const ir_node *block, ir_node *irn) {
410         ir_node *in, *load, *other, *left, *right;
411         int      is_cand = 0, cand;
412         int arity;
413
414         if (is_ia32_Ld(irn) || is_ia32_St(irn) ||
415                 is_ia32_vfild(irn) || is_ia32_vfist(irn) ||
416                 is_ia32_GetST0(irn) || is_ia32_SetST0(irn) || is_ia32_xStoreSimple(irn))
417                 return 0;
418
419         if(get_ia32_frame_ent(irn) != NULL)
420                 return IA32_AM_CAND_NONE;
421
422         left  = get_irn_n(irn, 2);
423         arity = get_irn_arity(irn);
424         if(get_ia32_am_arity(irn) == ia32_am_binary) {
425                 /* binary op */
426                 right = get_irn_n(irn, 3);
427         } else {
428                 assert(get_ia32_am_arity(irn) == ia32_am_unary);
429                 /* unary op */
430                 right = left;
431         }
432
433         in = left;
434
435         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
436 #ifndef AGGRESSIVE_AM
437                 int n;
438                 n         = ia32_get_irn_n_edges(in);
439                 is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */
440 #else
441                 is_cand   = 1;
442 #endif
443
444                 load  = get_Proj_pred(in);
445                 other = right;
446
447                 /* 8bit Loads are not supported (for binary ops),
448                  * they cannot be used with every register */
449                 if (get_ia32_am_arity(irn) == ia32_am_binary &&
450                                 get_mode_size_bits(get_ia32_ls_mode(load)) < 16) {
451                         is_cand = 0;
452                 }
453
454                 /* If there is a data dependency of other irn from load: cannot use AM */
455                 if (is_cand && get_nodes_block(other) == block) {
456                         other   = skip_Proj(other);
457                         is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
458                         /* this could happen in loops */
459                         is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
460                 }
461         }
462
463         cand    = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
464         in      = right;
465         is_cand = 0;
466
467         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
468 #ifndef AGGRESSIVE_AM
469                 int n;
470                 n         = ia32_get_irn_n_edges(in);
471                 is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */
472 #else
473                 is_cand = 1;
474 #endif
475
476                 load  = get_Proj_pred(in);
477                 other = left;
478
479                 /* 8bit Loads are not supported, they cannot be used with every register */
480                 if (get_mode_size_bits(get_ia32_ls_mode(load)) < 16)
481                         is_cand = 0;
482
483                 /* If there is a data dependency of other irn from load: cannot use load */
484                 if (is_cand && get_nodes_block(other) == block) {
485                         other   = skip_Proj(other);
486                         is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
487                         /* this could happen in loops */
488                         is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
489                 }
490         }
491
492         cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
493
494         /* if the irn has a frame entity: we do not use address mode */
495         return cand;
496 }
497
498 /**
499  * Compares the base and index addr and the load/store entities
500  * and returns 1 if they are equal.
501  */
502 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
503                                                                         const ir_node *addr_b, const ir_node *addr_i)
504 {
505         if(get_irn_n(load, 0) != addr_b)
506                 return 0;
507         if(get_irn_n(load, 1) != addr_i)
508                 return 0;
509
510         if(get_ia32_frame_ent(load) != get_ia32_frame_ent(store))
511                 return 0;
512
513         if(get_ia32_am_sc(load) != get_ia32_am_sc(store))
514                 return 0;
515         if(is_ia32_am_sc_sign(load) != is_ia32_am_sc_sign(store))
516                 return 0;
517         if(get_ia32_am_offs_int(load) != get_ia32_am_offs_int(store))
518                 return 0;
519         if(get_ia32_ls_mode(load) != get_ia32_ls_mode(store))
520                 return 0;
521
522         return 1;
523 }
524
525 typedef enum _ia32_take_lea_attr {
526         IA32_LEA_ATTR_NONE  = 0,
527         IA32_LEA_ATTR_BASE  = (1 << 0),
528         IA32_LEA_ATTR_INDEX = (1 << 1),
529         IA32_LEA_ATTR_OFFS  = (1 << 2),
530         IA32_LEA_ATTR_SCALE = (1 << 3),
531         IA32_LEA_ATTR_AMSC  = (1 << 4),
532         IA32_LEA_ATTR_FENT  = (1 << 5)
533 } ia32_take_lea_attr;
534
535 /**
536  * Decides if we have to keep the LEA operand or if we can assimilate it.
537  */
538 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
539                 int have_am_sc, ia32_code_gen_t *cg)
540 {
541         ir_entity *irn_ent  = get_ia32_frame_ent(irn);
542         ir_entity *lea_ent  = get_ia32_frame_ent(lea);
543         int        ret_val  = 0;
544         int        is_noreg_base  = be_is_NoReg(cg, base);
545         int        is_noreg_index = be_is_NoReg(cg, index);
546         ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
547
548         /* If the Add and the LEA both have a different frame entity set: keep */
549         if (irn_ent && lea_ent && (irn_ent != lea_ent))
550                 return IA32_LEA_ATTR_NONE;
551         else if (! irn_ent && lea_ent)
552                 ret_val |= IA32_LEA_ATTR_FENT;
553
554         /* If the Add and the LEA both have already an address mode symconst: keep */
555         if (have_am_sc && get_ia32_am_sc(lea))
556                 return IA32_LEA_ATTR_NONE;
557         else if (get_ia32_am_sc(lea))
558                 ret_val |= IA32_LEA_ATTR_AMSC;
559
560         /* Check the different base-index combinations */
561
562         if (! is_noreg_base && ! is_noreg_index) {
563                 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
564                 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
565                         if (am_flav & ia32_O)
566                                 ret_val |= IA32_LEA_ATTR_OFFS;
567
568                         ret_val |= IA32_LEA_ATTR_BASE;
569                 }
570                 else
571                         return IA32_LEA_ATTR_NONE;
572         }
573         else if (! is_noreg_base && is_noreg_index) {
574                 /* Base is set but index not */
575                 if (base == lea) {
576                         /* Base points to LEA: assimilate everything */
577                         if (am_flav & ia32_O)
578                                 ret_val |= IA32_LEA_ATTR_OFFS;
579                         if (am_flav & ia32_S)
580                                 ret_val |= IA32_LEA_ATTR_SCALE;
581                         if (am_flav & ia32_I)
582                                 ret_val |= IA32_LEA_ATTR_INDEX;
583
584                         ret_val |= IA32_LEA_ATTR_BASE;
585                 }
586                 else if (am_flav & ia32_B ? 0 : 1) {
587                         /* Base is not the LEA but the LEA is an index only calculation: assimilate */
588                         if (am_flav & ia32_O)
589                                 ret_val |= IA32_LEA_ATTR_OFFS;
590                         if (am_flav & ia32_S)
591                                 ret_val |= IA32_LEA_ATTR_SCALE;
592
593                         ret_val |= IA32_LEA_ATTR_INDEX;
594                 }
595                 else
596                         return IA32_LEA_ATTR_NONE;
597         }
598         else if (is_noreg_base && ! is_noreg_index) {
599                 /* Index is set but not base */
600                 if (index == lea) {
601                         /* Index points to LEA: assimilate everything */
602                         if (am_flav & ia32_O)
603                                 ret_val |= IA32_LEA_ATTR_OFFS;
604                         if (am_flav & ia32_S)
605                                 ret_val |= IA32_LEA_ATTR_SCALE;
606                         if (am_flav & ia32_B)
607                                 ret_val |= IA32_LEA_ATTR_BASE;
608
609                         ret_val |= IA32_LEA_ATTR_INDEX;
610                 }
611                 else if (am_flav & ia32_I ? 0 : 1) {
612                         /* Index is not the LEA but the LEA is a base only calculation: assimilate */
613                         if (am_flav & ia32_O)
614                                 ret_val |= IA32_LEA_ATTR_OFFS;
615                         if (am_flav & ia32_S)
616                                 ret_val |= IA32_LEA_ATTR_SCALE;
617
618                         ret_val |= IA32_LEA_ATTR_BASE;
619                 }
620                 else
621                         return IA32_LEA_ATTR_NONE;
622         }
623         else {
624                 assert(0 && "There must have been set base or index");
625         }
626
627         return ret_val;
628 }
629
630 /**
631  * Adds res before irn into schedule if irn was scheduled.
632  * @param irn  The schedule point
633  * @param res  The node to be scheduled
634  */
635 static INLINE void try_add_to_sched(ir_node *irn, ir_node *res) {
636         if (sched_is_scheduled(irn))
637                 sched_add_before(irn, res);
638 }
639
640 /**
641  * Removes node from schedule if it is not used anymore. If irn is a mode_T node
642  * all it's Projs are removed as well.
643  * @param irn  The irn to be removed from schedule
644  */
645 static INLINE void try_remove_from_sched(ir_node *node)
646 {
647         if(get_irn_mode(node) == mode_T) {
648                 const ir_edge_t *edge, *next;
649                 foreach_out_edge_safe(node, edge, next) {
650                         ir_node *proj = get_edge_src_irn(edge);
651                         try_remove_from_sched(proj);
652                 }
653         }
654
655         if(get_irn_n_edges(node) != 0)
656                 return;
657
658         if (sched_is_scheduled(node)) {
659                 sched_remove(node);
660         }
661
662         be_kill_node(node);
663 }
664
665 /**
666  * Folds Add or Sub to LEA if possible
667  */
668 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn) {
669         ir_graph   *irg        = get_irn_irg(irn);
670         dbg_info   *dbg_info   = get_irn_dbg_info(irn);
671         ir_node    *block      = get_nodes_block(irn);
672         ir_node    *res        = irn;
673         ir_node    *shift      = NULL;
674         ir_node    *lea_o      = NULL;
675         ir_node    *lea        = NULL;
676         long        offs       = 0;
677         long        offs_cnst  = 0;
678         long        offs_lea   = 0;
679         int         scale      = 0;
680         int         isadd      = 0;
681         int         dolea      = 0;
682         int         have_am_sc = 0;
683         int         am_sc_sign = 0;
684         ir_entity  *am_sc      = NULL;
685         ir_entity  *lea_ent    = NULL;
686         ir_node    *noreg      = ia32_new_NoReg_gp(cg);
687         ir_node    *left, *right, *temp;
688         ir_node    *base, *index;
689         int consumed_left_shift;
690         ia32_am_flavour_t am_flav;
691
692         if (is_ia32_Add(irn))
693                 isadd = 1;
694
695         left  = get_irn_n(irn, 2);
696         right = get_irn_n(irn, 3);
697
698         /* "normalize" arguments in case of add with two operands */
699         if  (isadd && ! be_is_NoReg(cg, right)) {
700                 /* put LEA == ia32_am_O as right operand */
701                 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
702                         set_irn_n(irn, 2, right);
703                         set_irn_n(irn, 3, left);
704                         temp  = left;
705                         left  = right;
706                         right = temp;
707                 }
708
709                 /* put LEA != ia32_am_O as left operand */
710                 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
711                         set_irn_n(irn, 2, right);
712                         set_irn_n(irn, 3, left);
713                         temp  = left;
714                         left  = right;
715                         right = temp;
716                 }
717
718                 /* put SHL as left operand iff left is NOT a LEA */
719                 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
720                         set_irn_n(irn, 2, right);
721                         set_irn_n(irn, 3, left);
722                         temp  = left;
723                         left  = right;
724                         right = temp;
725                 }
726         }
727
728         base    = left;
729         index   = noreg;
730         offs    = 0;
731         scale   = 0;
732         am_flav = 0;
733
734         /* check for operation with immediate */
735         if (is_ia32_ImmConst(irn)) {
736                 tarval *tv = get_ia32_Immop_tarval(irn);
737
738                 DBG((dbg, LEVEL_1, "\tfound op with imm const"));
739
740                 offs_cnst = get_tarval_long(tv);
741                 dolea     = 1;
742         }
743         else if (isadd && is_ia32_ImmSymConst(irn)) {
744                 DBG((dbg, LEVEL_1, "\tfound op with imm symconst"));
745
746                 have_am_sc = 1;
747                 dolea      = 1;
748                 am_sc      = get_ia32_Immop_symconst(irn);
749                 am_sc_sign = is_ia32_am_sc_sign(irn);
750         }
751
752         /* determine the operand which needs to be checked */
753         temp = be_is_NoReg(cg, right) ? left : right;
754
755         /* check if right operand is AMConst (LEA with ia32_am_O)  */
756         /* but we can only eat it up if there is no other symconst */
757         /* because the linker won't accept two symconsts           */
758         if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
759                 DBG((dbg, LEVEL_1, "\tgot op with LEA am_O"));
760
761                 offs_lea   = get_ia32_am_offs_int(temp);
762                 am_sc      = get_ia32_am_sc(temp);
763                 am_sc_sign = is_ia32_am_sc_sign(temp);
764                 have_am_sc = 1;
765                 dolea      = 1;
766                 lea_o      = temp;
767
768                 if (temp == base)
769                         base = noreg;
770                 else if (temp == right)
771                         right = noreg;
772         }
773
774         if (isadd) {
775                 /* default for add -> make right operand to index */
776                 index               = right;
777                 dolea               = 1;
778                 consumed_left_shift = -1;
779
780                 DBG((dbg, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
781
782                 /* determine the operand which needs to be checked */
783                 temp = left;
784                 if (is_ia32_Lea(left)) {
785                         temp = right;
786                         consumed_left_shift = 0;
787                 }
788
789                 /* check for SHL 1,2,3 */
790                 if (pred_is_specific_node(temp, is_ia32_Shl)) {
791                         ir_node *right = get_irn_n(temp, n_ia32_Shl_right);
792
793                         if (is_ia32_Immediate(right)) {
794                                 const ia32_immediate_attr_t *attr
795                                         = get_ia32_immediate_attr_const(right);
796                                 long shiftval = attr->offset;
797
798                                 if (shiftval <= 3) {
799                                         index               = get_irn_n(temp, 2);
800                                         consumed_left_shift = consumed_left_shift < 0 ? 1 : 0;
801                                         shift = temp;
802                                         scale = shiftval;
803
804                                         DBG((dbg, LEVEL_1, "\tgot scaled index %+F\n", index));
805                                 }
806                         }
807                 }
808
809                 /* fix base */
810                 if (! be_is_NoReg(cg, index)) {
811                         /* if we have index, but left == right -> no base */
812                         if (left == right) {
813                                 base = noreg;
814                         }
815                         else if (consumed_left_shift == 1) {
816                                 /* -> base is right operand  */
817                                 base = (right == lea_o) ? noreg : right;
818                         }
819                 }
820         }
821
822         /* Try to assimilate a LEA as left operand */
823         if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
824                 /* check if we can assimilate the LEA */
825                 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
826
827                 if (take_attr == IA32_LEA_ATTR_NONE) {
828                         DBG((dbg, LEVEL_1, "\tleave old LEA, creating new one\n"));
829                 }
830                 else {
831                         DBG((dbg, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
832                         lea = left; /* for statistics */
833
834                         if (take_attr & IA32_LEA_ATTR_OFFS)
835                                 offs = get_ia32_am_offs_int(left);
836
837                         if (take_attr & IA32_LEA_ATTR_AMSC) {
838                                 am_sc      = get_ia32_am_sc(left);
839                                 have_am_sc = 1;
840                                 am_sc_sign = is_ia32_am_sc_sign(left);
841                         }
842
843                         if (take_attr & IA32_LEA_ATTR_SCALE)
844                                 scale = get_ia32_am_scale(left);
845
846                         if (take_attr & IA32_LEA_ATTR_BASE)
847                                 base = get_irn_n(left, 0);
848
849                         if (take_attr & IA32_LEA_ATTR_INDEX)
850                                 index = get_irn_n(left, 1);
851
852                         if (take_attr & IA32_LEA_ATTR_FENT)
853                                 lea_ent = get_ia32_frame_ent(left);
854                 }
855         }
856
857         /* ok, we can create a new LEA */
858         if (dolea) {
859                 res = new_rd_ia32_Lea(dbg_info, irg, block, base, index);
860                 /* we don't want stuff before the barrier... */
861                 if(be_is_NoReg(cg, base) && be_is_NoReg(cg, index)) {
862                         add_irn_dep(res, get_irg_frame(irg));
863                 }
864
865                 /* add the old offset of a previous LEA */
866                 add_ia32_am_offs_int(res, offs);
867
868                 /* add the new offset */
869                 if (isadd) {
870                         add_ia32_am_offs_int(res, offs_cnst);
871                         add_ia32_am_offs_int(res, offs_lea);
872                 } else {
873                         /* either lea_O-cnst, -cnst or -lea_O  */
874                         if (offs_cnst != 0) {
875                                 add_ia32_am_offs_int(res, offs_lea);
876                                 add_ia32_am_offs_int(res, -offs_cnst);
877                         } else {
878                                 add_ia32_am_offs_int(res, offs_lea);
879                         }
880                 }
881
882                 /* set the address mode symconst */
883                 if (have_am_sc) {
884                         set_ia32_am_sc(res, am_sc);
885                         if (am_sc_sign)
886                                 set_ia32_am_sc_sign(res);
887                 }
888
889                 /* copy the frame entity (could be set in case of Add */
890                 /* which was a FrameAddr) */
891                 if (lea_ent != NULL) {
892                         set_ia32_frame_ent(res, lea_ent);
893                         set_ia32_use_frame(res);
894                 } else {
895                         set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
896                         if(is_ia32_use_frame(irn))
897                                 set_ia32_use_frame(res);
898                 }
899
900                 /* set scale */
901                 set_ia32_am_scale(res, scale);
902
903                 am_flav = ia32_am_N;
904                 /* determine new am flavour */
905                 if (offs || offs_cnst || offs_lea || have_am_sc) {
906                         am_flav |= ia32_O;
907                 }
908                 if (! be_is_NoReg(cg, base)) {
909                         am_flav |= ia32_B;
910                 }
911                 if (! be_is_NoReg(cg, index)) {
912                         am_flav |= ia32_I;
913                 }
914                 if (scale > 0) {
915                         am_flav |= ia32_S;
916                 }
917                 set_ia32_am_flavour(res, am_flav);
918
919                 set_ia32_op_type(res, ia32_AddrModeS);
920
921                 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
922
923                 DBG((dbg, LEVEL_1, "\tLEA [%+F + %+F * %d + %d]\n", base, index, scale, get_ia32_am_offs_int(res)));
924
925                 assert(irn && "Couldn't find result proj");
926
927                 /* get the result Proj of the Add/Sub */
928                 try_add_to_sched(irn, res);
929
930                 /* exchange the old op with the new LEA */
931                 try_remove_from_sched(irn);
932                 exchange(irn, res);
933
934                 /* we will exchange it, report here before the Proj is created */
935                 if (shift && lea && lea_o) {
936                         try_remove_from_sched(shift);
937                         try_remove_from_sched(lea);
938                         try_remove_from_sched(lea_o);
939                         DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
940                 } else if (shift && lea) {
941                         try_remove_from_sched(shift);
942                         try_remove_from_sched(lea);
943                         DBG_OPT_LEA3(irn, lea, shift, res);
944                 } else if (shift && lea_o) {
945                         try_remove_from_sched(shift);
946                         try_remove_from_sched(lea_o);
947                         DBG_OPT_LEA3(irn, lea_o, shift, res);
948                 } else if (lea && lea_o) {
949                         try_remove_from_sched(lea);
950                         try_remove_from_sched(lea_o);
951                         DBG_OPT_LEA3(irn, lea_o, lea, res);
952                 } else if (shift) {
953                         try_remove_from_sched(shift);
954                         DBG_OPT_LEA2(irn, shift, res);
955                 } else if (lea) {
956                         try_remove_from_sched(lea);
957                         DBG_OPT_LEA2(irn, lea, res);
958                 } else if (lea_o) {
959                         try_remove_from_sched(lea_o);
960                         DBG_OPT_LEA2(irn, lea_o, res);
961                 } else {
962                         DBG_OPT_LEA1(irn, res);
963                 }
964         }
965
966         return res;
967 }
968
969
970 /**
971  * Merges a Load/Store node with a LEA.
972  * @param irn The Load/Store node
973  * @param lea The LEA
974  */
975 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
976         ir_entity *irn_ent = get_ia32_frame_ent(irn);
977         ir_entity *lea_ent = get_ia32_frame_ent(lea);
978
979         /* If the irn and the LEA both have a different frame entity set: do not merge */
980         if (irn_ent != NULL && lea_ent != NULL && (irn_ent != lea_ent))
981                 return;
982         else if (irn_ent == NULL && lea_ent != NULL) {
983                 set_ia32_frame_ent(irn, lea_ent);
984                 set_ia32_use_frame(irn);
985         }
986
987         /* get the AM attributes from the LEA */
988         add_ia32_am_offs_int(irn, get_ia32_am_offs_int(lea));
989         set_ia32_am_scale(irn, get_ia32_am_scale(lea));
990         set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
991
992         set_ia32_am_sc(irn, get_ia32_am_sc(lea));
993         if (is_ia32_am_sc_sign(lea))
994                 set_ia32_am_sc_sign(irn);
995
996         set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
997
998         /* set base and index */
999         set_irn_n(irn, 0, get_irn_n(lea, 0));
1000         set_irn_n(irn, 1, get_irn_n(lea, 1));
1001
1002         try_remove_from_sched(lea);
1003
1004         /* clear remat flag */
1005         set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1006
1007         if (is_ia32_Ld(irn))
1008                 DBG_OPT_LOAD_LEA(lea, irn);
1009         else
1010                 DBG_OPT_STORE_LEA(lea, irn);
1011
1012 }
1013
1014 /**
1015  * Sets new_right index of irn to right and new_left index to left.
1016  * Also exchange left and right
1017  */
1018 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right,
1019                                 int new_left, int new_right)
1020 {
1021         ir_node *temp;
1022
1023         set_irn_n(irn, new_right, *right);
1024         set_irn_n(irn, new_left, *left);
1025
1026         temp   = *left;
1027         *left  = *right;
1028         *right = temp;
1029
1030         /* this is only needed for Compares, but currently ALL nodes
1031          * have this attribute :-) */
1032         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1033 }
1034
1035 /**
1036  * Performs address calculation optimization (create LEAs if possible)
1037  */
1038 static void optimize_lea(ia32_code_gen_t *cg, ir_node *irn) {
1039         if (! is_ia32_irn(irn))
1040                 return;
1041
1042         /* Following cases can occur:                                  */
1043         /* - Sub (l, imm) -> LEA [base - offset]                       */
1044         /* - Sub (l, r == LEA with ia32_am_O)   -> LEA [base - offset] */
1045         /* - Add (l, imm) -> LEA [base + offset]                       */
1046         /* - Add (l, r == LEA with ia32_am_O)  -> LEA [base + offset]  */
1047         /* - Add (l == LEA with ia32_am_O, r)  -> LEA [base + offset]  */
1048         /* - Add (l, r) -> LEA [base + index * scale]                  */
1049         /*              with scale > 1 iff l/r == shl (1,2,3)          */
1050         if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1051                 ir_node *res;
1052
1053                 if(!is_addr_candidate(irn))
1054                         return;
1055
1056                 DBG((dbg, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1057                 res = fold_addr(cg, irn);
1058
1059                 if (res != irn)
1060                         DB((dbg, LEVEL_1, "transformed into %+F\n", res));
1061                 else
1062                         DB((dbg, LEVEL_1, "not transformed\n"));
1063         } else if (is_ia32_Ld(irn) || is_ia32_St(irn)) {
1064                 /* - Load  -> LEA into Load  } TODO: If the LEA is used by more than one Load/Store */
1065                 /* - Store -> LEA into Store }       it might be better to keep the LEA             */
1066                 ir_node *left = get_irn_n(irn, 0);
1067
1068                 if (is_ia32_Lea(left)) {
1069                         const ir_edge_t *edge, *ne;
1070                         ir_node *src;
1071
1072                         /* merge all Loads/Stores connected to this LEA with the LEA */
1073                         foreach_out_edge_safe(left, edge, ne) {
1074                                 src = get_edge_src_irn(edge);
1075
1076                                 if (src && (get_edge_src_pos(edge) == 0) && (is_ia32_Ld(src) || is_ia32_St(src))) {
1077                                         DBG((dbg, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1078                                         if (! is_ia32_got_lea(src))
1079                                                 merge_loadstore_lea(src, left);
1080                                         set_ia32_got_lea(src);
1081                                 }
1082                         }
1083                 }
1084         }
1085 }
1086
1087 static void optimize_conv_store(ir_node *node)
1088 {
1089         ir_node *pred;
1090         ir_mode *conv_mode;
1091         ir_mode *store_mode;
1092
1093         if(!is_ia32_Store(node) && !is_ia32_Store8Bit(node))
1094                 return;
1095
1096         pred = get_irn_n(node, 2);
1097         if(!is_ia32_Conv_I2I(pred) && !is_ia32_Conv_I2I8Bit(pred))
1098                 return;
1099
1100         /* the store only stores the lower bits, so we only need the conv
1101          * it it shrinks the mode */
1102         conv_mode  = get_ia32_ls_mode(pred);
1103         store_mode = get_ia32_ls_mode(node);
1104         if(get_mode_size_bits(conv_mode) < get_mode_size_bits(store_mode))
1105                 return;
1106
1107         set_irn_n(node, 2, get_irn_n(pred, 2));
1108         if(get_irn_n_edges(pred) == 0) {
1109                 be_kill_node(pred);
1110         }
1111 }
1112
1113 static void optimize_load_conv(ir_node *node)
1114 {
1115         ir_node *pred, *predpred;
1116         ir_mode *load_mode;
1117         ir_mode *conv_mode;
1118
1119         if (!is_ia32_Conv_I2I(node) && !is_ia32_Conv_I2I8Bit(node))
1120                 return;
1121
1122         pred = get_irn_n(node, 2);
1123         if(!is_Proj(pred))
1124                 return;
1125
1126         predpred = get_Proj_pred(pred);
1127         if(!is_ia32_Load(predpred))
1128                 return;
1129
1130         /* the load is sign extending the upper bits, so we only need the conv
1131          * if it shrinks the mode */
1132         load_mode = get_ia32_ls_mode(predpred);
1133         conv_mode = get_ia32_ls_mode(node);
1134         if(get_mode_size_bits(conv_mode) < get_mode_size_bits(load_mode))
1135                 return;
1136
1137         if(get_mode_sign(conv_mode) != get_mode_sign(load_mode)) {
1138                 /* change the load if it has only 1 user */
1139                 if(get_irn_n_edges(pred) == 1) {
1140                         ir_mode *newmode;
1141                         if(get_mode_sign(conv_mode)) {
1142                                 newmode = find_signed_mode(load_mode);
1143                         } else {
1144                                 newmode = find_unsigned_mode(load_mode);
1145                         }
1146                         assert(newmode != NULL);
1147                         set_ia32_ls_mode(predpred, newmode);
1148                 } else {
1149                         /* otherwise we have to keep the conv */
1150                         return;
1151                 }
1152         }
1153
1154         /* kill the conv */
1155         exchange(node, pred);
1156 }
1157
1158 static void optimize_conv_conv(ir_node *node)
1159 {
1160         ir_node *pred;
1161         ir_mode *pred_mode;
1162         ir_mode *conv_mode;
1163
1164         if (!is_ia32_Conv_I2I(node) && !is_ia32_Conv_I2I8Bit(node))
1165                 return;
1166
1167         assert(n_ia32_Conv_I2I_val == n_ia32_Conv_I2I8Bit_val);
1168         pred = get_irn_n(node, n_ia32_Conv_I2I_val);
1169         if(!is_ia32_Conv_I2I(pred) && !is_ia32_Conv_I2I8Bit(pred))
1170                 return;
1171
1172         /* we know that after a conv, the upper bits are sign extended
1173          * so we only need the 2nd conv if it shrinks the mode */
1174         conv_mode = get_ia32_ls_mode(node);
1175         pred_mode = get_ia32_ls_mode(pred);
1176         if(get_mode_size_bits(conv_mode) < get_mode_size_bits(pred_mode))
1177                 return;
1178
1179         /* we can't eliminate an upconv signed->unsigned  */
1180         if (get_mode_size_bits(conv_mode) != get_mode_size_bits(pred_mode) &&
1181                 !get_mode_sign(conv_mode) && get_mode_sign(pred_mode))
1182                 return;
1183
1184         /* kill the conv */
1185         exchange(node, pred);
1186 }
1187
1188 static void optimize_node(ir_node *node, void *env)
1189 {
1190         ia32_code_gen_t *cg = env;
1191
1192         optimize_load_conv(node);
1193         optimize_conv_store(node);
1194         optimize_conv_conv(node);
1195         optimize_lea(cg, node);
1196 }
1197
1198 /**
1199  * Checks for address mode patterns and performs the
1200  * necessary transformations.
1201  * This function is called by a walker.
1202  */
1203 static void optimize_am(ir_node *irn, void *env) {
1204         ia32_am_opt_env_t *am_opt_env = env;
1205         ia32_code_gen_t   *cg         = am_opt_env->cg;
1206         ir_graph          *irg        = get_irn_irg(irn);
1207         heights_t         *h          = am_opt_env->h;
1208         ir_node           *block, *left, *right;
1209         ir_node           *store, *load, *mem_proj;
1210         ir_node           *addr_b, *addr_i;
1211         int               need_exchange_on_fail = 0;
1212         ia32_am_type_t    am_support;
1213         ia32_am_arity_t   am_arity;
1214         ia32_am_cand_t cand;
1215         ia32_am_cand_t orig_cand;
1216         int               dest_possible;
1217         int               source_possible;
1218
1219         static const arch_register_req_t dest_out_reg_req_0 = {
1220                 arch_register_req_type_none,
1221                 NULL,        /* regclass */
1222                 NULL,        /* limit bitset */
1223                 -1,          /* same pos */
1224                 -1           /* different pos */
1225         };
1226         static const arch_register_req_t *dest_am_out_reqs[] = {
1227                 &dest_out_reg_req_0
1228         };
1229
1230         if (!is_ia32_irn(irn) || is_ia32_Ld(irn) || is_ia32_St(irn))
1231                 return;
1232         if (is_ia32_Lea(irn))
1233                 return;
1234
1235         am_support = get_ia32_am_support(irn);
1236         am_arity   = get_ia32_am_arity(irn);
1237         block = get_nodes_block(irn);
1238
1239         /* fold following patterns:
1240          * - op -> Load into AMop with am_Source
1241          *   conditions:
1242          *     - op is am_Source capable AND
1243          *     - the Load is only used by this op AND
1244          *     - the Load is in the same block
1245          * - Store -> op -> Load  into AMop with am_Dest
1246          *   conditions:
1247          *     - op is am_Dest capable AND
1248          *     - the Store uses the same address as the Load AND
1249          *     - the Load is only used by this op AND
1250          *     - the Load and Store are in the same block AND
1251          *     - nobody else uses the result of the op
1252          */
1253         if (am_support == ia32_am_None)
1254                 return;
1255
1256         assert(am_arity == ia32_am_unary || am_arity == ia32_am_binary);
1257
1258         cand = is_am_candidate(h, block, irn);
1259         if (cand == IA32_AM_CAND_NONE)
1260                 return;
1261
1262         orig_cand = cand;
1263         DBG((dbg, LEVEL_1, "\tfound address mode candidate %+F (candleft %d candright %d)... \n", irn,
1264              cand & IA32_AM_CAND_LEFT, cand & IA32_AM_CAND_RIGHT));
1265
1266         left  = get_irn_n(irn, 2);
1267         if (am_arity == ia32_am_unary) {
1268                 right = left;
1269                 assert(cand == IA32_AM_CAND_BOTH);
1270         } else {
1271                 right = get_irn_n(irn, 3);
1272         }
1273
1274         dest_possible = am_support & ia32_am_Dest ? 1 : 0;
1275         source_possible = am_support & ia32_am_Source ? 1 : 0;
1276
1277         DBG((dbg, LEVEL_2, "\tdest_possible %d source_possible %d ... \n", dest_possible, source_possible));
1278
1279         if (dest_possible) {
1280                 addr_b = NULL;
1281                 addr_i = NULL;
1282                 store  = NULL;
1283
1284                 /* we should only have 1 user which is a store */
1285                 if (ia32_get_irn_n_edges(irn) == 1) {
1286                         ir_node *succ = get_edge_src_irn(get_irn_out_edge_first(irn));
1287
1288                         if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1289                                 store  = succ;
1290                                 addr_b = get_irn_n(store, 0);
1291                                 addr_i = get_irn_n(store, 1);
1292                         }
1293                 }
1294
1295                 if (store == NULL) {
1296                         DBG((dbg, LEVEL_2, "\tno store found, not using dest_mode\n"));
1297                         dest_possible = 0;
1298                 }
1299         }
1300
1301         if (dest_possible) {
1302                 /* normalize nodes, we need the interesting load on the left side */
1303                 if (cand & IA32_AM_CAND_RIGHT) {
1304                         load = get_Proj_pred(right);
1305                         if (load_store_addr_is_equal(load, store, addr_b, addr_i)
1306                                         && node_is_ia32_comm(irn)) {
1307                                 DBG((dbg, LEVEL_2, "\texchanging left/right\n"));
1308                                 exchange_left_right(irn, &left, &right, 3, 2);
1309                                 need_exchange_on_fail ^= 1;
1310                                 if (cand == IA32_AM_CAND_RIGHT)
1311                                         cand = IA32_AM_CAND_LEFT;
1312                         }
1313                 }
1314         }
1315
1316         if (dest_possible) {
1317                 if(cand & IA32_AM_CAND_LEFT && is_Proj(left)) {
1318                         load = get_Proj_pred(left);
1319
1320 #ifndef AGGRESSIVE_AM
1321                         /* we have to be the only user of the load */
1322                         if (get_irn_n_edges(left) > 1) {
1323                                 DBG((dbg, LEVEL_2, "\tmatching load has too may users, not using dest_mode\n"));
1324                                 dest_possible = 0;
1325                         }
1326 #endif
1327                 } else {
1328                         DBG((dbg, LEVEL_2, "\tno matching load found, not using dest_mode"));
1329                         dest_possible = 0;
1330                 }
1331         }
1332
1333         if (dest_possible) {
1334                 /* the store has to use the loads memory or the same memory
1335                  * as the load */
1336                 ir_node *loadmem = get_irn_n(load, 2);
1337                 ir_node *storemem = get_irn_n(store, 3);
1338                 assert(get_irn_mode(loadmem) == mode_M);
1339                 assert(get_irn_mode(storemem) == mode_M);
1340                 /* TODO there could be a sync between store and load... */
1341                 if(storemem != loadmem && (!is_Proj(storemem) || get_Proj_pred(storemem) != load)) {
1342                         DBG((dbg, LEVEL_2, "\tload/store using different memories, not using dest_mode"));
1343                         dest_possible = 0;
1344                 }
1345         }
1346
1347         if (dest_possible) {
1348                 /* Compare Load and Store address */
1349                 if (!load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1350                         DBG((dbg, LEVEL_2, "\taddresses not equal, not using dest_mode"));
1351                         dest_possible = 0;
1352                 }
1353         }
1354
1355         if (dest_possible) {
1356                 ir_mode *lsmode = get_ia32_ls_mode(load);
1357                 if(get_mode_size_bits(lsmode) != 32) {
1358                         dest_possible = 0;
1359                 }
1360         }
1361
1362         if (dest_possible) {
1363                 /* all conditions fullfilled, do the transformation */
1364                 assert(cand & IA32_AM_CAND_LEFT);
1365
1366                 /* set new base, index and attributes */
1367                 set_irn_n(irn, 0, addr_b);
1368                 set_irn_n(irn, 1, addr_i);
1369                 add_ia32_am_offs_int(irn, get_ia32_am_offs_int(load));
1370                 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1371                 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1372                 set_ia32_op_type(irn, ia32_AddrModeD);
1373                 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1374                 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1375
1376                 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1377                 if (is_ia32_am_sc_sign(load))
1378                         set_ia32_am_sc_sign(irn);
1379
1380                 /* connect to Load memory and disconnect Load */
1381                 if (am_arity == ia32_am_binary) {
1382                         /* binary AMop */
1383                         set_irn_n(irn, 4, get_irn_n(load, 2));
1384                         set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1385                 } else {
1386                         /* unary AMop */
1387                         set_irn_n(irn, 3, get_irn_n(load, 2));
1388                         set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1389                 }
1390
1391                 /* change node mode and out register requirements */
1392                 set_irn_mode(irn, mode_M);
1393                 set_ia32_out_req_all(irn, dest_am_out_reqs);
1394
1395                 /* connect the memory Proj of the Store to the op */
1396                 edges_reroute(store, irn, irg);
1397
1398                 /* clear remat flag */
1399                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1400
1401                 try_remove_from_sched(store);
1402                 try_remove_from_sched(load);
1403                 DBG_OPT_AM_D(load, store, irn);
1404
1405                 DB((dbg, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1406                 need_exchange_on_fail = 0;
1407                 source_possible = 0;
1408         }
1409
1410         if (source_possible) {
1411                 /* normalize ops, we need the load on the right */
1412                 if(cand == IA32_AM_CAND_LEFT) {
1413                         if(node_is_ia32_comm(irn)) {
1414                                 exchange_left_right(irn, &left, &right, 3, 2);
1415                                 need_exchange_on_fail ^= 1;
1416                                 cand = IA32_AM_CAND_RIGHT;
1417                         } else {
1418                                 source_possible = 0;
1419                         }
1420                 }
1421         }
1422
1423         if (source_possible) {
1424                 /* all conditions fullfilled, do transform */
1425                 assert(cand & IA32_AM_CAND_RIGHT);
1426                 load = get_Proj_pred(right);
1427
1428                 if(get_irn_n_edges(load) > 1) {
1429                         source_possible = 0;
1430                 }
1431         }
1432
1433         if (source_possible) {
1434                 ir_mode *ls_mode = get_ia32_ls_mode(load);
1435                 if(get_mode_size_bits(ls_mode) != 32)
1436                         source_possible = 0;
1437
1438         }
1439
1440         if (source_possible) {
1441                 addr_b = get_irn_n(load, 0);
1442                 addr_i = get_irn_n(load, 1);
1443
1444                 /* set new base, index and attributes */
1445                 set_irn_n(irn, 0, addr_b);
1446                 set_irn_n(irn, 1, addr_i);
1447                 add_ia32_am_offs_int(irn, get_ia32_am_offs_int(load));
1448                 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1449                 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1450                 set_ia32_op_type(irn, ia32_AddrModeS);
1451                 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1452                 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1453
1454                 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1455                 if (is_ia32_am_sc_sign(load))
1456                         set_ia32_am_sc_sign(irn);
1457
1458                 /* clear remat flag */
1459                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1460
1461                 if (is_ia32_use_frame(load)) {
1462                         if(get_ia32_frame_ent(load) == NULL) {
1463                                 set_ia32_need_stackent(irn);
1464                         }
1465                         set_ia32_use_frame(irn);
1466                 }
1467
1468                 /* connect to Load memory and disconnect Load */
1469                 if (am_arity == ia32_am_binary) {
1470                         /* binary AMop */
1471                         set_irn_n(irn, 3, ia32_get_admissible_noreg(cg, irn, 3));
1472                         set_irn_n(irn, 4, get_irn_n(load, 2));
1473                 } else {
1474                         /* unary AMop */
1475                         set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1476                         set_irn_n(irn, 3, get_irn_n(load, 2));
1477                 }
1478
1479                 DBG_OPT_AM_S(load, irn);
1480
1481                 /* If Load has a memory Proj, connect it to the op */
1482                 mem_proj = ia32_get_proj_for_mode(load, mode_M);
1483                 if (mem_proj != NULL) {
1484                         ir_node *res_proj;
1485                         ir_mode *mode = get_irn_mode(irn);
1486
1487                         res_proj = new_rd_Proj(get_irn_dbg_info(irn), irg,
1488                                                get_nodes_block(irn), new_Unknown(mode_T),
1489                                                mode, 0);
1490                         set_irn_mode(irn, mode_T);
1491                         edges_reroute(irn, res_proj, irg);
1492                         set_Proj_pred(res_proj, irn);
1493
1494                         set_Proj_pred(mem_proj, irn);
1495                         set_Proj_proj(mem_proj, 1);
1496
1497                         if(sched_is_scheduled(irn)) {
1498                                 sched_add_after(irn, res_proj);
1499                                 sched_add_after(irn, mem_proj);
1500                         }
1501                 }
1502
1503                 try_remove_from_sched(load);
1504                 need_exchange_on_fail = 0;
1505
1506                 /* immediate are only allowed on the right side */
1507                 if(is_ia32_Immediate(left)) {
1508                         exchange_left_right(irn, &left, &right, 3, 2);
1509                 }
1510
1511                 DB((dbg, LEVEL_1, "merged with %+F into source AM\n", load));
1512         }
1513
1514         /* was exchanged but optimize failed: exchange back */
1515         if (need_exchange_on_fail) {
1516                 exchange_left_right(irn, &left, &right, 3, 2);
1517         }
1518 }
1519
1520 /**
1521  * Performs conv and address mode optimization.
1522  */
1523 void ia32_optimize_graph(ia32_code_gen_t *cg) {
1524         /* if we are supposed to do AM or LEA optimization: recalculate edges */
1525         if (! (cg->opt & (IA32_OPT_DOAM | IA32_OPT_LEA))) {
1526                 /* no optimizations at all */
1527                 return;
1528         }
1529
1530         /* beware: we cannot optimize LEA and AM in one run because */
1531         /*         LEA optimization adds new nodes to the irg which */
1532         /*         invalidates the phase data                       */
1533
1534         if (cg->opt & IA32_OPT_LEA) {
1535                 irg_walk_blkwise_graph(cg->irg, NULL, optimize_node, cg);
1536         }
1537
1538         if (cg->dump)
1539                 be_dump(cg->irg, "-lea", dump_ir_block_graph_sched);
1540
1541         /* hack for now, so these don't get created during optimize, because then
1542          * they will be unknown to the heights module
1543          */
1544         ia32_new_NoReg_gp(cg);
1545         ia32_new_NoReg_fp(cg);
1546         ia32_new_NoReg_vfp(cg);
1547
1548         if (cg->opt & IA32_OPT_DOAM) {
1549                 /* we need height information for am optimization */
1550                 heights_t *h = heights_new(cg->irg);
1551                 ia32_am_opt_env_t env;
1552
1553                 env.cg = cg;
1554                 env.h  = h;
1555
1556                 irg_walk_blkwise_graph(cg->irg, NULL, optimize_am, &env);
1557
1558                 heights_free(h);
1559         }
1560 }
1561
1562 void ia32_init_optimize(void)
1563 {
1564         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.optimize");
1565 }