2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Cliff Click's Combined Analysis/Optimization
23 * @author Michael Beck
26 * This is a slightly enhanced version of Cliff Clicks combo algorithm
27 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28 * - supports all Firm direct (by a data edge) identities except Mux
29 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31 * - let Cmp nodes calculate Top like all othe data nodes: this would let
32 * Mux nodes to calculate Unknown instead of taking the true result
33 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34 * IFF the predecessor changed its type. Because nodes are initialized with Top
35 * this never happens, let all Proj(Cond) be unreachable.
36 * We avoid this condition by the same way we work around Phi: whenever a Block
37 * node is placed on the list, place its Cond nodes (and because they are Tuple
38 * all its Proj-nodes either on the cprop list)
39 * Especially, this changes the meaning of Click's example:
54 * using Click's version while is silent with our.
55 * - support for global congruences is implemented but not tested yet
57 * Note further that we use the terminology from Click's work here, which is different
58 * in some cases from Firm terminology. Especially, Click's type is a
59 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
65 #include "iroptimize.h"
73 #include "irgraph_t.h"
80 #include "iropt_dbg.h"
84 #include "irnodeset.h"
91 /* define this to check that all type translations are monotone */
92 #define VERIFY_MONOTONE
94 /* define this to check the consistency of partitions */
95 #define CHECK_PARTITIONS
97 typedef struct node_t node_t;
98 typedef struct partition_t partition_t;
99 typedef struct opcode_key_t opcode_key_t;
100 typedef struct listmap_entry_t listmap_entry_t;
102 /** The type of the compute function. */
103 typedef void (*compute_func)(node_t *node);
108 struct opcode_key_t {
109 ir_opcode code; /**< The Firm opcode. */
110 ir_mode *mode; /**< The mode of all nodes in the partition. */
111 int arity; /**< The arity of this opcode (needed for Phi etc. */
113 long proj; /**< For Proj nodes, its proj number */
114 ir_entity *ent; /**< For Sel Nodes, its entity */
119 * An entry in the list_map.
121 struct listmap_entry_t {
122 void *id; /**< The id. */
123 node_t *list; /**< The associated list for this id. */
124 listmap_entry_t *next; /**< Link to the next entry in the map. */
127 /** We must map id's to lists. */
128 typedef struct listmap_t {
129 set *map; /**< Map id's to listmap_entry_t's */
130 listmap_entry_t *values; /**< List of all values in the map. */
134 * A lattice element. Because we handle constants and symbolic constants different, we
135 * have to use this union.
146 ir_node *node; /**< The IR-node itself. */
147 list_head node_list; /**< Double-linked list of leader/follower entries. */
148 list_head cprop_list; /**< Double-linked partition.cprop list. */
149 partition_t *part; /**< points to the partition this node belongs to */
150 node_t *next; /**< Next node on local list (partition.touched, fallen). */
151 node_t *race_next; /**< Next node on race list. */
152 lattice_elem_t type; /**< The associated lattice element "type". */
153 int max_user_input; /**< Maximum input number of Def-Use edges. */
154 int next_edge; /**< Index of the next Def-Use edge to use. */
155 int n_followers; /**< Number of Follower in the outs set. */
156 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
157 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
158 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
159 unsigned is_follower:1; /**< Set, if this node is a follower. */
160 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
164 * A partition containing congruent nodes.
167 list_head Leader; /**< The head of partition Leader node list. */
168 list_head Follower; /**< The head of partition Follower node list. */
169 list_head cprop; /**< The head of partition.cprop list. */
170 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
171 partition_t *wl_next; /**< Next entry in the work list if any. */
172 partition_t *touched_next; /**< Points to the next partition in the touched set. */
173 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
174 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
175 node_t *touched; /**< The partition.touched set of this partition. */
176 unsigned n_leader; /**< Number of entries in this partition.Leader. */
177 unsigned n_touched; /**< Number of entries in the partition.touched. */
178 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
179 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
180 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
181 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
182 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
184 partition_t *dbg_next; /**< Link all partitions for debugging */
185 unsigned nr; /**< A unique number for (what-)mapping, >0. */
189 typedef struct environment_t {
190 struct obstack obst; /**< obstack to allocate data structures. */
191 partition_t *worklist; /**< The work list. */
192 partition_t *cprop; /**< The constant propagation list. */
193 partition_t *touched; /**< the touched set. */
194 partition_t *initial; /**< The initial partition. */
195 set *opcode2id_map; /**< The opcodeMode->id map. */
196 pmap *type2id_map; /**< The type->id map. */
197 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
198 int end_idx; /**< -1 for local and 0 for global congruences. */
199 int lambda_input; /**< Captured argument for lambda_partition(). */
200 unsigned modified:1; /**< Set, if the graph was modified. */
201 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
202 /* options driving the optimization */
203 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
204 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
206 partition_t *dbg_list; /**< List of all partitions. */
210 /** Type of the what function. */
211 typedef void *(*what_func)(const node_t *node, environment_t *env);
213 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
214 #define set_irn_node(irn, node) set_irn_link(irn, node)
216 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
217 #undef tarval_unreachable
218 #define tarval_unreachable tarval_top
221 /** The debug module handle. */
222 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
224 /** The what reason. */
225 DEBUG_ONLY(static const char *what_reason;)
227 /** Next partition number. */
228 DEBUG_ONLY(static unsigned part_nr = 0);
230 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
231 static tarval *tarval_UNKNOWN;
234 static node_t *identity(node_t *node);
236 #ifdef CHECK_PARTITIONS
240 static void check_partition(const partition_t *T) {
244 list_for_each_entry(node_t, node, &T->Leader, node_list) {
245 assert(node->is_follower == 0);
246 assert(node->flagged == 0);
247 assert(node->part == T);
250 assert(n == T->n_leader);
252 list_for_each_entry(node_t, node, &T->Follower, node_list) {
253 assert(node->is_follower == 1);
254 assert(node->flagged == 0);
255 assert(node->part == T);
257 } /* check_partition */
260 * check that all leader nodes in the partition have the same opcode.
262 static void check_opcode(const partition_t *Z) {
267 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
268 ir_node *irn = node->node;
271 key.code = get_irn_opcode(irn);
272 key.mode = get_irn_mode(irn);
273 key.arity = get_irn_arity(irn);
277 switch (get_irn_opcode(irn)) {
279 key.u.proj = get_Proj_proj(irn);
282 key.u.ent = get_Sel_entity(irn);
289 assert(key.code == get_irn_opcode(irn));
290 assert(key.mode == get_irn_mode(irn));
291 assert(key.arity == get_irn_arity(irn));
293 switch (get_irn_opcode(irn)) {
295 assert(key.u.proj == get_Proj_proj(irn));
298 assert(key.u.ent == get_Sel_entity(irn));
307 static void check_all_partitions(environment_t *env) {
312 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
314 if (! P->type_is_T_or_C)
316 list_for_each_entry(node_t, node, &P->Follower, node_list) {
317 node_t *leader = identity(node);
319 assert(leader != node && leader->part == node->part);
328 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
331 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
332 for (e = list; e != NULL; e = NEXT(e)) {
333 assert(e->part == Z);
336 } /* ido_check_list */
339 * Check a local list.
341 static void check_list(const node_t *list, const partition_t *Z) {
342 do_check_list(list, offsetof(node_t, next), Z);
346 #define check_partition(T)
347 #define check_list(list, Z)
348 #define check_all_partitions(env)
349 #endif /* CHECK_PARTITIONS */
352 static inline lattice_elem_t get_partition_type(const partition_t *X);
355 * Dump partition to output.
357 static void dump_partition(const char *msg, const partition_t *part) {
360 lattice_elem_t type = get_partition_type(part);
362 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
363 msg, part->nr, part->type_is_T_or_C ? "*" : "",
364 part->n_leader, type));
365 list_for_each_entry(node_t, node, &part->Leader, node_list) {
366 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
369 if (! list_empty(&part->Follower)) {
370 DB((dbg, LEVEL_2, "\n---\n "));
372 list_for_each_entry(node_t, node, &part->Follower, node_list) {
373 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
377 DB((dbg, LEVEL_2, "\n}\n"));
378 } /* dump_partition */
383 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
387 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
389 DB((dbg, LEVEL_3, "%s = {\n ", msg));
390 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
391 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
394 DB((dbg, LEVEL_3, "\n}\n"));
402 static void dump_race_list(const char *msg, const node_t *list) {
403 do_dump_list(msg, list, offsetof(node_t, race_next));
404 } /* dump_race_list */
407 * Dumps a local list.
409 static void dump_list(const char *msg, const node_t *list) {
410 do_dump_list(msg, list, offsetof(node_t, next));
414 * Dump all partitions.
416 static void dump_all_partitions(const environment_t *env) {
417 const partition_t *P;
419 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
420 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
421 dump_partition("", P);
422 } /* dump_all_partitions */
427 static void dump_split_list(const partition_t *list) {
428 const partition_t *p;
430 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
431 for (p = list; p != NULL; p = p->split_next)
432 DB((dbg, LEVEL_2, "part%u, ", p->nr));
433 DB((dbg, LEVEL_2, "\n}\n"));
434 } /* dump_split_list */
437 * Dump partition and type for a node.
439 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
440 ir_node *irn = local != NULL ? local : n;
441 node_t *node = get_irn_node(irn);
443 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
445 } /* dump_partition_hook */
448 #define dump_partition(msg, part)
449 #define dump_race_list(msg, list)
450 #define dump_list(msg, list)
451 #define dump_all_partitions(env)
452 #define dump_split_list(list)
455 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
457 * Verify that a type transition is monotone
459 static void verify_type(const lattice_elem_t old_type, node_t *node) {
460 if (old_type.tv == node->type.tv) {
464 if (old_type.tv == tarval_top) {
465 /* from Top down-to is always allowed */
468 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
472 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
476 #define verify_type(old_type, node)
480 * Compare two pointer values of a listmap.
482 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
483 const listmap_entry_t *e1 = elt;
484 const listmap_entry_t *e2 = key;
487 return e1->id != e2->id;
488 } /* listmap_cmp_ptr */
491 * Initializes a listmap.
493 * @param map the listmap
495 static void listmap_init(listmap_t *map) {
496 map->map = new_set(listmap_cmp_ptr, 16);
501 * Terminates a listmap.
503 * @param map the listmap
505 static void listmap_term(listmap_t *map) {
510 * Return the associated listmap entry for a given id.
512 * @param map the listmap
513 * @param id the id to search for
515 * @return the associated listmap entry for the given id
517 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
518 listmap_entry_t key, *entry;
523 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
525 if (entry->list == NULL) {
526 /* a new entry, put into the list */
527 entry->next = map->values;
534 * Calculate the hash value for an opcode map entry.
536 * @param entry an opcode map entry
538 * @return a hash value for the given opcode map entry
540 static unsigned opcode_hash(const opcode_key_t *entry) {
541 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
545 * Compare two entries in the opcode map.
547 static int cmp_opcode(const void *elt, const void *key, size_t size) {
548 const opcode_key_t *o1 = elt;
549 const opcode_key_t *o2 = key;
552 return o1->code != o2->code || o1->mode != o2->mode ||
553 o1->arity != o2->arity ||
554 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
558 * Compare two Def-Use edges for input position.
560 static int cmp_def_use_edge(const void *a, const void *b) {
561 const ir_def_use_edge *ea = a;
562 const ir_def_use_edge *eb = b;
564 /* no overrun, because range is [-1, MAXINT] */
565 return ea->pos - eb->pos;
566 } /* cmp_def_use_edge */
569 * We need the Def-Use edges sorted.
571 static void sort_irn_outs(node_t *node) {
572 ir_node *irn = node->node;
573 int n_outs = get_irn_n_outs(irn);
576 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
578 node->max_user_input = irn->out[n_outs].pos;
579 } /* sort_irn_outs */
582 * Return the type of a node.
584 * @param irn an IR-node
586 * @return the associated type of this node
588 static inline lattice_elem_t get_node_type(const ir_node *irn) {
589 return get_irn_node(irn)->type;
590 } /* get_node_type */
593 * Return the tarval of a node.
595 * @param irn an IR-node
597 * @return the associated type of this node
599 static inline tarval *get_node_tarval(const ir_node *irn) {
600 lattice_elem_t type = get_node_type(irn);
602 if (is_tarval(type.tv))
604 return tarval_bottom;
605 } /* get_node_type */
608 * Add a partition to the worklist.
610 static inline void add_to_worklist(partition_t *X, environment_t *env) {
611 assert(X->on_worklist == 0);
612 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
613 X->wl_next = env->worklist;
616 } /* add_to_worklist */
619 * Create a new empty partition.
621 * @param env the environment
623 * @return a newly allocated partition
625 static inline partition_t *new_partition(environment_t *env) {
626 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
628 INIT_LIST_HEAD(&part->Leader);
629 INIT_LIST_HEAD(&part->Follower);
630 INIT_LIST_HEAD(&part->cprop);
631 INIT_LIST_HEAD(&part->cprop_X);
632 part->wl_next = NULL;
633 part->touched_next = NULL;
634 part->cprop_next = NULL;
635 part->split_next = NULL;
636 part->touched = NULL;
639 part->max_user_inputs = 0;
640 part->on_worklist = 0;
641 part->on_touched = 0;
643 part->type_is_T_or_C = 0;
645 part->dbg_next = env->dbg_list;
646 env->dbg_list = part;
647 part->nr = part_nr++;
651 } /* new_partition */
654 * Get the first node from a partition.
656 static inline node_t *get_first_node(const partition_t *X) {
657 return list_entry(X->Leader.next, node_t, node_list);
658 } /* get_first_node */
661 * Return the type of a partition (assuming partition is non-empty and
662 * all elements have the same type).
664 * @param X a partition
666 * @return the type of the first element of the partition
668 static inline lattice_elem_t get_partition_type(const partition_t *X) {
669 const node_t *first = get_first_node(X);
671 } /* get_partition_type */
674 * Creates a partition node for the given IR-node and place it
675 * into the given partition.
677 * @param irn an IR-node
678 * @param part a partition to place the node in
679 * @param env the environment
681 * @return the created node
683 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
684 /* create a partition node and place it in the partition */
685 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
687 INIT_LIST_HEAD(&node->node_list);
688 INIT_LIST_HEAD(&node->cprop_list);
692 node->race_next = NULL;
693 node->type.tv = tarval_top;
694 node->max_user_input = 0;
696 node->n_followers = 0;
697 node->on_touched = 0;
700 node->is_follower = 0;
702 set_irn_node(irn, node);
704 list_add_tail(&node->node_list, &part->Leader);
708 } /* create_partition_node */
711 * Pre-Walker, initialize all Nodes' type to U or top and place
712 * all nodes into the TOP partition.
714 static void create_initial_partitions(ir_node *irn, void *ctx) {
715 environment_t *env = ctx;
716 partition_t *part = env->initial;
719 node = create_partition_node(irn, part, env);
721 if (node->max_user_input > part->max_user_inputs)
722 part->max_user_inputs = node->max_user_input;
725 set_Block_phis(irn, NULL);
727 } /* create_initial_partitions */
730 * Post-Walker, collect all Block-Phi lists, set Cond.
732 static void init_block_phis(ir_node *irn, void *ctx) {
736 add_Block_phi(get_nodes_block(irn), irn);
738 } /* init_block_phis */
741 * Add a node to the entry.partition.touched set and
742 * node->partition to the touched set if not already there.
745 * @param env the environment
747 static inline void add_to_touched(node_t *y, environment_t *env) {
748 if (y->on_touched == 0) {
749 partition_t *part = y->part;
751 y->next = part->touched;
756 if (part->on_touched == 0) {
757 part->touched_next = env->touched;
759 part->on_touched = 1;
762 check_list(part->touched, part);
764 } /* add_to_touched */
767 * Place a node on the cprop list.
770 * @param env the environment
772 static void add_to_cprop(node_t *y, environment_t *env) {
775 /* Add y to y.partition.cprop. */
776 if (y->on_cprop == 0) {
777 partition_t *Y = y->part;
778 ir_node *irn = y->node;
780 /* place Conds and all its Projs on the cprop_X list */
781 if (is_Cond(skip_Proj(irn)))
782 list_add_tail(&y->cprop_list, &Y->cprop_X);
784 list_add_tail(&y->cprop_list, &Y->cprop);
787 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
789 /* place its partition on the cprop list */
790 if (Y->on_cprop == 0) {
791 Y->cprop_next = env->cprop;
797 if (get_irn_mode(irn) == mode_T) {
798 /* mode_T nodes always produce tarval_bottom, so we must explicitly
799 add it's Proj's to get constant evaluation to work */
802 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
803 node_t *proj = get_irn_node(get_irn_out(irn, i));
805 add_to_cprop(proj, env);
807 } else if (is_Block(irn)) {
808 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
809 * if someone placed the block. The Block is only placed if the reachability
810 * changes, and this must be re-evaluated in compute_Phi(). */
812 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
813 node_t *p = get_irn_node(phi);
814 add_to_cprop(p, env);
820 * Update the worklist: If Z is on worklist then add Z' to worklist.
821 * Else add the smaller of Z and Z' to worklist.
823 * @param Z the Z partition
824 * @param Z_prime the Z' partition, a previous part of Z
825 * @param env the environment
827 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
828 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
829 add_to_worklist(Z_prime, env);
831 add_to_worklist(Z, env);
833 } /* update_worklist */
836 * Make all inputs to x no longer be F.def_use edges.
840 static void move_edges_to_leader(node_t *x) {
841 ir_node *irn = x->node;
844 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
845 node_t *pred = get_irn_node(get_irn_n(irn, i));
850 n = get_irn_n_outs(p);
851 for (j = 1; j <= pred->n_followers; ++j) {
852 if (p->out[j].pos == i && p->out[j].use == irn) {
853 /* found a follower edge to x, move it to the Leader */
854 ir_def_use_edge edge = p->out[j];
856 /* remove this edge from the Follower set */
857 p->out[j] = p->out[pred->n_followers];
860 /* sort it into the leader set */
861 for (k = pred->n_followers + 2; k <= n; ++k) {
862 if (p->out[k].pos >= edge.pos)
864 p->out[k - 1] = p->out[k];
866 /* place the new edge here */
867 p->out[k - 1] = edge;
869 /* edge found and moved */
874 } /* move_edges_to_leader */
877 * Split a partition that has NO followers by a local list.
879 * @param Z partition to split
880 * @param g a (non-empty) node list
881 * @param env the environment
883 * @return a new partition containing the nodes of g
885 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
886 partition_t *Z_prime;
891 dump_partition("Splitting ", Z);
892 dump_list("by list ", g);
896 /* Remove g from Z. */
897 for (node = g; node != NULL; node = node->next) {
898 assert(node->part == Z);
899 list_del(&node->node_list);
902 assert(n < Z->n_leader);
905 /* Move g to a new partition, Z'. */
906 Z_prime = new_partition(env);
908 for (node = g; node != NULL; node = node->next) {
909 list_add_tail(&node->node_list, &Z_prime->Leader);
910 node->part = Z_prime;
911 if (node->max_user_input > max_input)
912 max_input = node->max_user_input;
914 Z_prime->max_user_inputs = max_input;
915 Z_prime->n_leader = n;
918 check_partition(Z_prime);
920 /* for now, copy the type info tag, it will be adjusted in split_by(). */
921 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
923 update_worklist(Z, Z_prime, env);
925 dump_partition("Now ", Z);
926 dump_partition("Created new ", Z_prime);
928 } /* split_no_followers */
931 * Make the Follower -> Leader transition for a node.
935 static void follower_to_leader(node_t *n) {
936 assert(n->is_follower == 1);
938 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
940 move_edges_to_leader(n);
941 list_del(&n->node_list);
942 list_add_tail(&n->node_list, &n->part->Leader);
944 } /* follower_to_leader */
947 * The environment for one race step.
949 typedef struct step_env {
950 node_t *initial; /**< The initial node list. */
951 node_t *unwalked; /**< The unwalked node list. */
952 node_t *walked; /**< The walked node list. */
953 int index; /**< Next index of Follower use_def edge. */
954 unsigned side; /**< side number. */
958 * Return non-zero, if a input is a real follower
960 * @param irn the node to check
961 * @param input number of the input
963 static int is_real_follower(const ir_node *irn, int input) {
966 switch (get_irn_opcode(irn)) {
969 /* ignore the Confirm bound input */
975 /* ignore the Mux sel input */
980 /* dead inputs are not follower edges */
981 ir_node *block = get_nodes_block(irn);
982 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
984 if (pred->type.tv == tarval_unreachable)
994 /* only a Sub x,0 / Shift x,0 might be a follower */
1001 pred = get_irn_node(get_irn_n(irn, input));
1002 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1006 pred = get_irn_node(get_irn_n(irn, input));
1007 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1011 pred = get_irn_node(get_irn_n(irn, input));
1012 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1017 /* all inputs are followers */
1020 assert(!"opcode not implemented yet");
1024 } /* is_real_follower */
1027 * Do one step in the race.
1029 static int step(step_env *env) {
1032 if (env->initial != NULL) {
1033 /* Move node from initial to unwalked */
1035 env->initial = n->race_next;
1037 n->race_next = env->unwalked;
1043 while (env->unwalked != NULL) {
1044 /* let n be the first node in unwalked */
1046 while (env->index < n->n_followers) {
1047 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1049 /* let m be n.F.def_use[index] */
1050 node_t *m = get_irn_node(edge->use);
1052 assert(m->is_follower);
1054 * Some inputs, like the get_Confirm_bound are NOT
1055 * real followers, sort them out.
1057 if (! is_real_follower(m->node, edge->pos)) {
1063 /* only followers from our partition */
1064 if (m->part != n->part)
1067 if ((m->flagged & env->side) == 0) {
1068 m->flagged |= env->side;
1070 if (m->flagged != 3) {
1071 /* visited the first time */
1072 /* add m to unwalked not as first node (we might still need to
1073 check for more follower node */
1074 m->race_next = n->race_next;
1078 /* else already visited by the other side and on the other list */
1081 /* move n to walked */
1082 env->unwalked = n->race_next;
1083 n->race_next = env->walked;
1091 * Clear the flags from a list and check for
1092 * nodes that where touched from both sides.
1094 * @param list the list
1096 static int clear_flags(node_t *list) {
1100 for (n = list; n != NULL; n = n->race_next) {
1101 if (n->flagged == 3) {
1102 /* we reach a follower from both sides, this will split congruent
1103 * inputs and make it a leader. */
1104 follower_to_leader(n);
1113 * Split a partition by a local list using the race.
1115 * @param pX pointer to the partition to split, might be changed!
1116 * @param gg a (non-empty) node list
1117 * @param env the environment
1119 * @return a new partition containing the nodes of gg
1121 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1122 partition_t *X = *pX;
1123 partition_t *X_prime;
1126 node_t *g, *h, *node, *t;
1127 int max_input, transitions, winner, shf;
1129 DEBUG_ONLY(static int run = 0;)
1131 DB((dbg, LEVEL_2, "Run %d ", run++));
1132 if (list_empty(&X->Follower)) {
1133 /* if the partition has NO follower, we can use the fast
1134 splitting algorithm. */
1135 return split_no_followers(X, gg, env);
1137 /* else do the race */
1139 dump_partition("Splitting ", X);
1140 dump_list("by list ", gg);
1142 INIT_LIST_HEAD(&tmp);
1144 /* Remove gg from X.Leader and put into g */
1146 for (node = gg; node != NULL; node = node->next) {
1147 assert(node->part == X);
1148 assert(node->is_follower == 0);
1150 list_del(&node->node_list);
1151 list_add_tail(&node->node_list, &tmp);
1152 node->race_next = g;
1157 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1158 node->race_next = h;
1161 /* restore X.Leader */
1162 list_splice(&tmp, &X->Leader);
1164 senv[0].initial = g;
1165 senv[0].unwalked = NULL;
1166 senv[0].walked = NULL;
1170 senv[1].initial = h;
1171 senv[1].unwalked = NULL;
1172 senv[1].walked = NULL;
1177 * Some informations on the race that are not stated clearly in Click's
1179 * 1) A follower stays on the side that reach him first.
1180 * 2) If the other side reches a follower, if will be converted to
1181 * a leader. /This must be done after the race is over, else the
1182 * edges we are iterating on are renumbered./
1183 * 3) /New leader might end up on both sides./
1184 * 4) /If one side ends up with new Leaders, we must ensure that
1185 * they can split out by opcode, hence we have to put _every_
1186 * partition with new Leader nodes on the cprop list, as
1187 * opcode splitting is done by split_by() at the end of
1188 * constant propagation./
1191 if (step(&senv[0])) {
1195 if (step(&senv[1])) {
1200 assert(senv[winner].initial == NULL);
1201 assert(senv[winner].unwalked == NULL);
1203 /* clear flags from walked/unwalked */
1205 transitions = clear_flags(senv[0].unwalked) << shf;
1206 transitions |= clear_flags(senv[0].walked) << shf;
1208 transitions |= clear_flags(senv[1].unwalked) << shf;
1209 transitions |= clear_flags(senv[1].walked) << shf;
1211 dump_race_list("winner ", senv[winner].walked);
1213 /* Move walked_{winner} to a new partition, X'. */
1214 X_prime = new_partition(env);
1217 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1218 list_del(&node->node_list);
1219 node->part = X_prime;
1220 if (node->is_follower) {
1221 list_add_tail(&node->node_list, &X_prime->Follower);
1223 list_add_tail(&node->node_list, &X_prime->Leader);
1226 if (node->max_user_input > max_input)
1227 max_input = node->max_user_input;
1229 X_prime->n_leader = n;
1230 X_prime->max_user_inputs = max_input;
1231 X->n_leader -= X_prime->n_leader;
1233 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1234 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1237 * Even if a follower was not checked by both sides, it might have
1238 * loose its congruence, so we need to check this case for all follower.
1240 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1241 if (identity(node) == node) {
1242 follower_to_leader(node);
1248 check_partition(X_prime);
1250 /* X' is the smaller part */
1251 add_to_worklist(X_prime, env);
1254 * If there where follower to leader transitions, ensure that the nodes
1255 * can be split out if necessary.
1257 if (transitions & 1) {
1258 /* place winner partition on the cprop list */
1259 if (X_prime->on_cprop == 0) {
1260 X_prime->cprop_next = env->cprop;
1261 env->cprop = X_prime;
1262 X_prime->on_cprop = 1;
1265 if (transitions & 2) {
1266 /* place other partition on the cprop list */
1267 if (X->on_cprop == 0) {
1268 X->cprop_next = env->cprop;
1274 dump_partition("Now ", X);
1275 dump_partition("Created new ", X_prime);
1277 /* we have to ensure that the partition containing g is returned */
1287 * Returns non-zero if the i'th input of a Phi node is live.
1289 * @param phi a Phi-node
1290 * @param i an input number
1292 * @return non-zero if the i'th input of the given Phi node is live
1294 static int is_live_input(ir_node *phi, int i) {
1296 ir_node *block = get_nodes_block(phi);
1297 ir_node *pred = get_Block_cfgpred(block, i);
1298 lattice_elem_t type = get_node_type(pred);
1300 return type.tv != tarval_unreachable;
1302 /* else it's the control input, always live */
1304 } /* is_live_input */
1307 * Return non-zero if a type is a constant.
1309 static int is_constant_type(lattice_elem_t type) {
1310 if (type.tv != tarval_bottom && type.tv != tarval_top)
1313 } /* is_constant_type */
1316 * Check whether a type is neither Top or a constant.
1317 * Note: U is handled like Top here, R is a constant.
1319 * @param type the type to check
1321 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1322 if (is_tarval(type.tv)) {
1323 if (type.tv == tarval_top)
1325 if (tarval_is_constant(type.tv))
1332 } /* type_is_neither_top_nor_const */
1335 * Collect nodes to the touched list.
1337 * @param list the list which contains the nodes that must be evaluated
1338 * @param idx the index of the def_use edge to evaluate
1339 * @param env the environment
1341 static void collect_touched(list_head *list, int idx, environment_t *env) {
1343 int end_idx = env->end_idx;
1345 list_for_each_entry(node_t, x, list, node_list) {
1349 /* leader edges start AFTER follower edges */
1350 x->next_edge = x->n_followers + 1;
1352 num_edges = get_irn_n_outs(x->node);
1354 /* for all edges in x.L.def_use_{idx} */
1355 while (x->next_edge <= num_edges) {
1356 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1359 /* check if we have necessary edges */
1360 if (edge->pos > idx)
1367 /* only non-commutative nodes */
1368 if (env->commutative &&
1369 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1372 /* ignore the "control input" for non-pinned nodes
1373 if we are running in GCSE mode */
1374 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1377 y = get_irn_node(succ);
1378 assert(get_irn_n(succ, idx) == x->node);
1380 /* ignore block edges touching followers */
1381 if (idx == -1 && y->is_follower)
1384 if (is_constant_type(y->type)) {
1385 ir_opcode code = get_irn_opcode(succ);
1386 if (code == iro_Sub || code == iro_Cmp)
1387 add_to_cprop(y, env);
1390 /* Partitions of constants should not be split simply because their Nodes have unequal
1391 functions or incongruent inputs. */
1392 if (type_is_neither_top_nor_const(y->type) &&
1393 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1394 add_to_touched(y, env);
1398 } /* collect_touched */
1401 * Collect commutative nodes to the touched list.
1403 * @param X the partition of the list
1404 * @param list the list which contains the nodes that must be evaluated
1405 * @param env the environment
1407 static void collect_commutative_touched(partition_t *X, list_head *list, environment_t *env) {
1412 list_for_each_entry(node_t, x, list, node_list) {
1415 num_edges = get_irn_n_outs(x->node);
1417 x->next_edge = x->n_followers + 1;
1419 /* for all edges in x.L.def_use_{idx} */
1420 while (x->next_edge <= num_edges) {
1421 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1424 /* check if we have necessary edges */
1434 /* only commutative nodes */
1435 if (!is_op_commutative(get_irn_op(succ)))
1438 y = get_irn_node(succ);
1439 if (is_constant_type(y->type)) {
1440 ir_opcode code = get_irn_opcode(succ);
1441 if (code == iro_Eor)
1442 add_to_cprop(y, env);
1445 /* Partitions of constants should not be split simply because their Nodes have unequal
1446 functions or incongruent inputs. */
1447 if (type_is_neither_top_nor_const(y->type)) {
1448 int other_idx = edge->pos ^ 1;
1449 node_t *other = get_irn_node(get_irn_n(succ, other_idx));
1450 int equal = X == other->part;
1453 * Note: op(a, a) is NOT congruent to op(a, b).
1454 * So, either all touch nodes must have both inputs congruent,
1455 * or not. We decide this by the first occurred node.
1461 if (both_input == equal)
1462 add_to_touched(y, env);
1466 } /* collect_commutative_touched */
1469 * Split the partitions if caused by the first entry on the worklist.
1471 * @param env the environment
1473 static void cause_splits(environment_t *env) {
1474 partition_t *X, *Z, *N;
1477 /* remove the first partition from the worklist */
1479 env->worklist = X->wl_next;
1482 dump_partition("Cause_split: ", X);
1484 if (env->commutative) {
1485 /* handle commutative nodes first */
1487 /* empty the touched set: already done, just clear the list */
1488 env->touched = NULL;
1490 collect_commutative_touched(X, &X->Leader, env);
1491 collect_commutative_touched(X, &X->Follower, env);
1493 for (Z = env->touched; Z != NULL; Z = N) {
1495 node_t *touched = Z->touched;
1496 unsigned n_touched = Z->n_touched;
1498 assert(Z->touched != NULL);
1500 /* beware, split might change Z */
1501 N = Z->touched_next;
1503 /* remove it from the touched set */
1506 /* Empty local Z.touched. */
1507 for (e = touched; e != NULL; e = e->next) {
1508 assert(e->is_follower == 0);
1514 if (0 < n_touched && n_touched < Z->n_leader) {
1515 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1516 split(&Z, touched, env);
1518 assert(n_touched <= Z->n_leader);
1522 /* combine temporary leader and follower list */
1523 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1524 /* empty the touched set: already done, just clear the list */
1525 env->touched = NULL;
1527 collect_touched(&X->Leader, idx, env);
1528 collect_touched(&X->Follower, idx, env);
1530 for (Z = env->touched; Z != NULL; Z = N) {
1532 node_t *touched = Z->touched;
1533 unsigned n_touched = Z->n_touched;
1535 assert(Z->touched != NULL);
1537 /* beware, split might change Z */
1538 N = Z->touched_next;
1540 /* remove it from the touched set */
1543 /* Empty local Z.touched. */
1544 for (e = touched; e != NULL; e = e->next) {
1545 assert(e->is_follower == 0);
1551 if (0 < n_touched && n_touched < Z->n_leader) {
1552 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1553 split(&Z, touched, env);
1555 assert(n_touched <= Z->n_leader);
1558 } /* cause_splits */
1561 * Implements split_by_what(): Split a partition by characteristics given
1562 * by the what function.
1564 * @param X the partition to split
1565 * @param What a function returning an Id for every node of the partition X
1566 * @param P a list to store the result partitions
1567 * @param env the environment
1571 static partition_t *split_by_what(partition_t *X, what_func What,
1572 partition_t **P, environment_t *env) {
1575 listmap_entry_t *iter;
1578 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1580 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1581 void *id = What(x, env);
1582 listmap_entry_t *entry;
1585 /* input not allowed, ignore */
1588 /* Add x to map[What(x)]. */
1589 entry = listmap_find(&map, id);
1590 x->next = entry->list;
1593 /* Let P be a set of Partitions. */
1595 /* for all sets S except one in the range of map do */
1596 for (iter = map.values; iter != NULL; iter = iter->next) {
1597 if (iter->next == NULL) {
1598 /* this is the last entry, ignore */
1603 /* Add SPLIT( X, S ) to P. */
1604 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1605 R = split(&X, S, env);
1615 } /* split_by_what */
1617 /** lambda n.(n.type) */
1618 static void *lambda_type(const node_t *node, environment_t *env) {
1620 return node->type.tv;
1623 /** lambda n.(n.opcode) */
1624 static void *lambda_opcode(const node_t *node, environment_t *env) {
1625 opcode_key_t key, *entry;
1626 ir_node *irn = node->node;
1628 key.code = get_irn_opcode(irn);
1629 key.mode = get_irn_mode(irn);
1630 key.arity = get_irn_arity(irn);
1634 switch (get_irn_opcode(irn)) {
1636 key.u.proj = get_Proj_proj(irn);
1639 key.u.ent = get_Sel_entity(irn);
1645 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1647 } /* lambda_opcode */
1649 /** lambda n.(n[i].partition) */
1650 static void *lambda_partition(const node_t *node, environment_t *env) {
1651 ir_node *skipped = skip_Proj(node->node);
1654 int i = env->lambda_input;
1656 if (i >= get_irn_arity(node->node)) {
1658 * We are outside the allowed range: This can happen even
1659 * if we have split by opcode first: doing so might move Followers
1660 * to Leaders and those will have a different opcode!
1661 * Note that in this case the partition is on the cprop list and will be
1667 /* ignore the "control input" for non-pinned nodes
1668 if we are running in GCSE mode */
1669 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1672 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1673 p = get_irn_node(pred);
1676 } /* lambda_partition */
1678 /** lambda n.(n[i].partition) for commutative nodes */
1679 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1680 ir_node *irn = node->node;
1681 ir_node *skipped = skip_Proj(irn);
1682 ir_node *pred, *left, *right;
1684 partition_t *pl, *pr;
1685 int i = env->lambda_input;
1687 if (i >= get_irn_arity(node->node)) {
1689 * We are outside the allowed range: This can happen even
1690 * if we have split by opcode first: doing so might move Followers
1691 * to Leaders and those will have a different opcode!
1692 * Note that in this case the partition is on the cprop list and will be
1698 /* ignore the "control input" for non-pinned nodes
1699 if we are running in GCSE mode */
1700 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1704 pred = get_irn_n(skipped, i);
1705 p = get_irn_node(pred);
1709 if (is_op_commutative(get_irn_op(irn))) {
1710 /* normalize partition order by returning the "smaller" on input 0,
1711 the "bigger" on input 1. */
1712 left = get_binop_left(irn);
1713 pl = get_irn_node(left)->part;
1714 right = get_binop_right(irn);
1715 pr = get_irn_node(right)->part;
1718 return pl < pr ? pl : pr;
1720 return pl > pr ? pl : pr;
1722 /* a not split out Follower */
1723 pred = get_irn_n(irn, i);
1724 p = get_irn_node(pred);
1728 } /* lambda_commutative_partition */
1731 * Returns true if a type is a constant (and NOT Top
1734 static int is_con(const lattice_elem_t type) {
1735 /* be conservative */
1736 if (is_tarval(type.tv))
1737 return tarval_is_constant(type.tv);
1738 return is_entity(type.sym.entity_p);
1742 * Implements split_by().
1744 * @param X the partition to split
1745 * @param env the environment
1747 static void split_by(partition_t *X, environment_t *env) {
1748 partition_t *I, *P = NULL;
1751 dump_partition("split_by", X);
1753 if (X->n_leader == 1) {
1754 /* we have only one leader, no need to split, just check it's type */
1755 node_t *x = get_first_node(X);
1756 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1760 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1761 P = split_by_what(X, lambda_type, &P, env);
1764 /* adjust the type tags, we have split partitions by type */
1765 for (I = P; I != NULL; I = I->split_next) {
1766 node_t *x = get_first_node(I);
1767 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1774 if (Y->n_leader > 1) {
1775 /* we do not want split the TOP or constant partitions */
1776 if (! Y->type_is_T_or_C) {
1777 partition_t *Q = NULL;
1779 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1780 Q = split_by_what(Y, lambda_opcode, &Q, env);
1787 if (Z->n_leader > 1) {
1788 const node_t *first = get_first_node(Z);
1789 int arity = get_irn_arity(first->node);
1791 what_func what = lambda_partition;
1792 DEBUG_ONLY(char buf[64];)
1794 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1795 what = lambda_commutative_partition;
1798 * BEWARE: during splitting by input 2 for instance we might
1799 * create new partitions which are different by input 1, so collect
1800 * them and split further.
1802 Z->split_next = NULL;
1805 for (input = arity - 1; input >= -1; --input) {
1807 partition_t *Z_prime = R;
1810 if (Z_prime->n_leader > 1) {
1811 env->lambda_input = input;
1812 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1813 DEBUG_ONLY(what_reason = buf;)
1814 S = split_by_what(Z_prime, what, &S, env);
1817 Z_prime->split_next = S;
1820 } while (R != NULL);
1825 } while (Q != NULL);
1828 } while (P != NULL);
1832 * (Re-)compute the type for a given node.
1834 * @param node the node
1836 static void default_compute(node_t *node) {
1838 ir_node *irn = node->node;
1840 /* if any of the data inputs have type top, the result is type top */
1841 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1842 ir_node *pred = get_irn_n(irn, i);
1843 node_t *p = get_irn_node(pred);
1845 if (p->type.tv == tarval_top) {
1846 node->type.tv = tarval_top;
1851 if (get_irn_mode(node->node) == mode_X)
1852 node->type.tv = tarval_reachable;
1854 node->type.tv = computed_value(irn);
1855 } /* default_compute */
1858 * (Re-)compute the type for a Block node.
1860 * @param node the node
1862 static void compute_Block(node_t *node) {
1864 ir_node *block = node->node;
1866 if (block == get_irg_start_block(current_ir_graph)) {
1867 /* start block is always reachable */
1868 node->type.tv = tarval_reachable;
1872 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1873 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1875 if (pred->type.tv == tarval_reachable) {
1876 /* A block is reachable, if at least of predecessor is reachable. */
1877 node->type.tv = tarval_reachable;
1881 node->type.tv = tarval_top;
1882 } /* compute_Block */
1885 * (Re-)compute the type for a Bad node.
1887 * @param node the node
1889 static void compute_Bad(node_t *node) {
1890 /* Bad nodes ALWAYS compute Top */
1891 node->type.tv = tarval_top;
1895 * (Re-)compute the type for an Unknown node.
1897 * @param node the node
1899 static void compute_Unknown(node_t *node) {
1900 /* While Unknown nodes should compute Top this is dangerous:
1901 * a Top input to a Cond would lead to BOTH control flows unreachable.
1902 * While this is correct in the given semantics, it would destroy the Firm
1905 * It would be safe to compute Top IF it can be assured, that only Cmp
1906 * nodes are inputs to Conds. We check that first.
1907 * This is the way Frontends typically build Firm, but some optimizations
1908 * (cond_eval for instance) might replace them by Phib's...
1910 node->type.tv = tarval_UNKNOWN;
1911 } /* compute_Unknown */
1914 * (Re-)compute the type for a Jmp node.
1916 * @param node the node
1918 static void compute_Jmp(node_t *node) {
1919 node_t *block = get_irn_node(get_nodes_block(node->node));
1921 node->type = block->type;
1925 * (Re-)compute the type for the Return node.
1927 * @param node the node
1929 static void compute_Return(node_t *node) {
1930 /* The Return node is NOT dead if it is in a reachable block.
1931 * This is already checked in compute(). so we can return
1932 * Reachable here. */
1933 node->type.tv = tarval_reachable;
1934 } /* compute_Return */
1937 * (Re-)compute the type for the End node.
1939 * @param node the node
1941 static void compute_End(node_t *node) {
1942 /* the End node is NOT dead of course */
1943 node->type.tv = tarval_reachable;
1947 * (Re-)compute the type for a Call.
1949 * @param node the node
1951 static void compute_Call(node_t *node) {
1953 * A Call computes always bottom, even if it has Unknown
1956 node->type.tv = tarval_bottom;
1957 } /* compute_Call */
1960 * (Re-)compute the type for a SymConst node.
1962 * @param node the node
1964 static void compute_SymConst(node_t *node) {
1965 ir_node *irn = node->node;
1966 node_t *block = get_irn_node(get_nodes_block(irn));
1968 if (block->type.tv == tarval_unreachable) {
1969 node->type.tv = tarval_top;
1972 switch (get_SymConst_kind(irn)) {
1973 case symconst_addr_ent:
1974 /* case symconst_addr_name: cannot handle this yet */
1975 node->type.sym = get_SymConst_symbol(irn);
1978 node->type.tv = computed_value(irn);
1980 } /* compute_SymConst */
1983 * (Re-)compute the type for a Phi node.
1985 * @param node the node
1987 static void compute_Phi(node_t *node) {
1989 ir_node *phi = node->node;
1990 lattice_elem_t type;
1992 /* if a Phi is in a unreachable block, its type is TOP */
1993 node_t *block = get_irn_node(get_nodes_block(phi));
1995 if (block->type.tv == tarval_unreachable) {
1996 node->type.tv = tarval_top;
2000 /* Phi implements the Meet operation */
2001 type.tv = tarval_top;
2002 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2003 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2004 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2006 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2007 /* ignore TOP inputs: We must check here for unreachable blocks,
2008 because Firm constants live in the Start Block are NEVER Top.
2009 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2010 comes from a unreachable input. */
2013 if (pred->type.tv == tarval_bottom) {
2014 node->type.tv = tarval_bottom;
2016 } else if (type.tv == tarval_top) {
2017 /* first constant found */
2019 } else if (type.tv != pred->type.tv) {
2020 /* different constants or tarval_bottom */
2021 node->type.tv = tarval_bottom;
2024 /* else nothing, constants are the same */
2030 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2032 * @param node the node
2034 static void compute_Add(node_t *node) {
2035 ir_node *sub = node->node;
2036 node_t *l = get_irn_node(get_Add_left(sub));
2037 node_t *r = get_irn_node(get_Add_right(sub));
2038 lattice_elem_t a = l->type;
2039 lattice_elem_t b = r->type;
2042 if (a.tv == tarval_top || b.tv == tarval_top) {
2043 node->type.tv = tarval_top;
2044 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2045 node->type.tv = tarval_bottom;
2047 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2048 must call tarval_add() first to handle this case! */
2049 if (is_tarval(a.tv)) {
2050 if (is_tarval(b.tv)) {
2051 node->type.tv = tarval_add(a.tv, b.tv);
2054 mode = get_tarval_mode(a.tv);
2055 if (a.tv == get_mode_null(mode)) {
2059 } else if (is_tarval(b.tv)) {
2060 mode = get_tarval_mode(b.tv);
2061 if (b.tv == get_mode_null(mode)) {
2066 node->type.tv = tarval_bottom;
2071 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2073 * @param node the node
2075 static void compute_Sub(node_t *node) {
2076 ir_node *sub = node->node;
2077 node_t *l = get_irn_node(get_Sub_left(sub));
2078 node_t *r = get_irn_node(get_Sub_right(sub));
2079 lattice_elem_t a = l->type;
2080 lattice_elem_t b = r->type;
2083 if (a.tv == tarval_top || b.tv == tarval_top) {
2084 node->type.tv = tarval_top;
2085 } else if (is_con(a) && is_con(b)) {
2086 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2087 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2088 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2090 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2093 node->type.tv = tarval_bottom;
2095 } else if (r->part == l->part &&
2096 (!mode_is_float(get_irn_mode(l->node)))) {
2098 * BEWARE: a - a is NOT always 0 for floating Point values, as
2099 * NaN op NaN = NaN, so we must check this here.
2101 ir_mode *mode = get_irn_mode(sub);
2102 tv = get_mode_null(mode);
2104 /* if the node was ONCE evaluated by all constants, but now
2105 this breaks AND we get from the argument partitions a different
2106 result, switch to bottom.
2107 This happens because initially all nodes are in the same partition ... */
2108 if (node->type.tv != tv)
2112 node->type.tv = tarval_bottom;
2117 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2119 * @param node the node
2121 static void compute_Eor(node_t *node) {
2122 ir_node *eor = node->node;
2123 node_t *l = get_irn_node(get_Eor_left(eor));
2124 node_t *r = get_irn_node(get_Eor_right(eor));
2125 lattice_elem_t a = l->type;
2126 lattice_elem_t b = r->type;
2129 if (a.tv == tarval_top || b.tv == tarval_top) {
2130 node->type.tv = tarval_top;
2131 } else if (is_con(a) && is_con(b)) {
2132 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2133 node->type.tv = tarval_eor(a.tv, b.tv);
2134 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2136 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2139 node->type.tv = tarval_bottom;
2141 } else if (r->part == l->part) {
2142 ir_mode *mode = get_irn_mode(eor);
2143 tv = get_mode_null(mode);
2145 /* if the node was ONCE evaluated by all constants, but now
2146 this breaks AND we get from the argument partitions a different
2147 result, switch to bottom.
2148 This happens because initially all nodes are in the same partition ... */
2149 if (node->type.tv != tv)
2153 node->type.tv = tarval_bottom;
2158 * (Re-)compute the type for Cmp.
2160 * @param node the node
2162 static void compute_Cmp(node_t *node) {
2163 ir_node *cmp = node->node;
2164 node_t *l = get_irn_node(get_Cmp_left(cmp));
2165 node_t *r = get_irn_node(get_Cmp_right(cmp));
2166 lattice_elem_t a = l->type;
2167 lattice_elem_t b = r->type;
2169 if (a.tv == tarval_top || b.tv == tarval_top) {
2170 node->type.tv = tarval_top;
2171 } else if (r->part == l->part) {
2172 /* both nodes congruent, we can probably do something */
2173 node->type.tv = tarval_b_true;
2174 } else if (is_con(a) && is_con(b)) {
2175 /* both nodes are constants, we can probably do something */
2176 node->type.tv = tarval_b_true;
2178 node->type.tv = tarval_bottom;
2183 * (Re-)compute the type for a Proj(Cmp).
2185 * @param node the node
2186 * @param cond the predecessor Cmp node
2188 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2189 ir_node *proj = node->node;
2190 node_t *l = get_irn_node(get_Cmp_left(cmp));
2191 node_t *r = get_irn_node(get_Cmp_right(cmp));
2192 lattice_elem_t a = l->type;
2193 lattice_elem_t b = r->type;
2194 pn_Cmp pnc = get_Proj_proj(proj);
2197 if (a.tv == tarval_top || b.tv == tarval_top) {
2198 node->type.tv = tarval_undefined;
2199 } else if (is_con(a) && is_con(b)) {
2200 default_compute(node);
2201 } else if (r->part == l->part &&
2202 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2204 * BEWARE: a == a is NOT always True for floating Point values, as
2205 * NaN != NaN is defined, so we must check this here.
2207 tv = pnc & pn_Cmp_Eq ? tarval_b_true: tarval_b_false;
2209 /* if the node was ONCE evaluated by all constants, but now
2210 this breaks AND we get from the argument partitions a different
2211 result, switch to bottom.
2212 This happens because initially all nodes are in the same partition ... */
2213 if (node->type.tv != tv)
2217 node->type.tv = tarval_bottom;
2219 } /* compute_Proj_Cmp */
2222 * (Re-)compute the type for a Proj(Cond).
2224 * @param node the node
2225 * @param cond the predecessor Cond node
2227 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2228 ir_node *proj = node->node;
2229 long pnc = get_Proj_proj(proj);
2230 ir_node *sel = get_Cond_selector(cond);
2231 node_t *selector = get_irn_node(sel);
2234 * Note: it is crucial for the monotony that the Proj(Cond)
2235 * are evaluates after all predecessors of the Cond selector are
2241 * Due to the fact that 0 is a const, the Cmp gets immediately
2242 * on the cprop list. It will be evaluated before x is evaluated,
2243 * might leaving x as Top. When later x is evaluated, the Cmp
2244 * might change its value.
2245 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2246 * gets R, and later changed to F if Cmp is evaluated to True!
2248 * We prevent this by putting Conds in an extra cprop_X queue, which
2249 * gets evaluated after the cprop queue is empty.
2251 * Note that this even happens with Click's original algorithm, if
2252 * Cmp(x, 0) is evaluated to True first and later changed to False
2253 * if x was Top first and later changed to a Const ...
2254 * It is unclear how Click solved that problem ...
2256 * However, in rare cases even this does not help, if a Top reaches
2257 * a compare through a Phi, than Proj(Cond) is evaluated changing
2258 * the type of the Phi to something other.
2259 * So, we take the last resort and bind the type to R once
2262 * (This might be even the way Click works around the whole problem).
2264 * Finally, we may miss some optimization possibilities due to this:
2269 * If Top reaches the if first, than we decide for != here.
2270 * If y later is evaluated to 0, we cannot revert this decision
2271 * and must live with both outputs enabled. If this happens,
2272 * we get an unresolved if (true) in the code ...
2274 * In Click's version where this decision is done at the Cmp,
2275 * the Cmp is NOT optimized away than (if y evaluated to 1
2276 * for instance) and we get a if (1 == 0) here ...
2278 * Both solutions are suboptimal.
2279 * At least, we could easily detect this problem and run
2280 * cf_opt() (or even combo) again :-(
2282 if (node->type.tv == tarval_reachable)
2285 if (get_irn_mode(sel) == mode_b) {
2287 if (pnc == pn_Cond_true) {
2288 if (selector->type.tv == tarval_b_false) {
2289 node->type.tv = tarval_unreachable;
2290 } else if (selector->type.tv == tarval_b_true) {
2291 node->type.tv = tarval_reachable;
2292 } else if (selector->type.tv == tarval_bottom) {
2293 node->type.tv = tarval_reachable;
2295 assert(selector->type.tv == tarval_top);
2296 if (tarval_UNKNOWN == tarval_top) {
2297 /* any condition based on Top is "!=" */
2298 node->type.tv = tarval_unreachable;
2300 node->type.tv = tarval_unreachable;
2304 assert(pnc == pn_Cond_false);
2306 if (selector->type.tv == tarval_b_false) {
2307 node->type.tv = tarval_reachable;
2308 } else if (selector->type.tv == tarval_b_true) {
2309 node->type.tv = tarval_unreachable;
2310 } else if (selector->type.tv == tarval_bottom) {
2311 node->type.tv = tarval_reachable;
2313 assert(selector->type.tv == tarval_top);
2314 if (tarval_UNKNOWN == tarval_top) {
2315 /* any condition based on Top is "!=" */
2316 node->type.tv = tarval_reachable;
2318 node->type.tv = tarval_unreachable;
2324 if (selector->type.tv == tarval_bottom) {
2325 node->type.tv = tarval_reachable;
2326 } else if (selector->type.tv == tarval_top) {
2327 if (tarval_UNKNOWN == tarval_top &&
2328 pnc == get_Cond_defaultProj(cond)) {
2329 /* a switch based of Top is always "default" */
2330 node->type.tv = tarval_reachable;
2332 node->type.tv = tarval_unreachable;
2335 long value = get_tarval_long(selector->type.tv);
2336 if (pnc == get_Cond_defaultProj(cond)) {
2337 /* default switch, have to check ALL other cases */
2340 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2341 ir_node *succ = get_irn_out(cond, i);
2345 if (value == get_Proj_proj(succ)) {
2346 /* we found a match, will NOT take the default case */
2347 node->type.tv = tarval_unreachable;
2351 /* all cases checked, no match, will take default case */
2352 node->type.tv = tarval_reachable;
2355 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2359 } /* compute_Proj_Cond */
2362 * (Re-)compute the type for a Proj-Node.
2364 * @param node the node
2366 static void compute_Proj(node_t *node) {
2367 ir_node *proj = node->node;
2368 ir_mode *mode = get_irn_mode(proj);
2369 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2370 ir_node *pred = get_Proj_pred(proj);
2372 if (block->type.tv == tarval_unreachable) {
2373 /* a Proj in a unreachable Block stay Top */
2374 node->type.tv = tarval_top;
2377 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2378 /* if the predecessor is Top, its Proj follow */
2379 node->type.tv = tarval_top;
2383 if (mode == mode_M) {
2384 /* mode M is always bottom */
2385 node->type.tv = tarval_bottom;
2388 if (mode != mode_X) {
2390 compute_Proj_Cmp(node, pred);
2392 default_compute(node);
2395 /* handle mode_X nodes */
2397 switch (get_irn_opcode(pred)) {
2399 /* the Proj_X from the Start is always reachable.
2400 However this is already handled at the top. */
2401 node->type.tv = tarval_reachable;
2404 compute_Proj_Cond(node, pred);
2407 default_compute(node);
2409 } /* compute_Proj */
2412 * (Re-)compute the type for a Confirm.
2414 * @param node the node
2416 static void compute_Confirm(node_t *node) {
2417 ir_node *confirm = node->node;
2418 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2420 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2421 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2423 if (is_con(bound->type)) {
2424 /* is equal to a constant */
2425 node->type = bound->type;
2429 /* a Confirm is a copy OR a Const */
2430 node->type = pred->type;
2431 } /* compute_Confirm */
2434 * (Re-)compute the type for a Max.
2436 * @param node the node
2438 static void compute_Max(node_t *node) {
2439 ir_node *op = node->node;
2440 node_t *l = get_irn_node(get_binop_left(op));
2441 node_t *r = get_irn_node(get_binop_right(op));
2442 lattice_elem_t a = l->type;
2443 lattice_elem_t b = r->type;
2445 if (a.tv == tarval_top || b.tv == tarval_top) {
2446 node->type.tv = tarval_top;
2447 } else if (is_con(a) && is_con(b)) {
2448 /* both nodes are constants, we can probably do something */
2450 /* this case handles SymConsts as well */
2453 ir_mode *mode = get_irn_mode(op);
2454 tarval *tv_min = get_mode_min(mode);
2458 else if (b.tv == tv_min)
2460 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2461 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2462 node->type.tv = a.tv;
2464 node->type.tv = b.tv;
2466 node->type.tv = tarval_bad;
2469 } else if (r->part == l->part) {
2470 /* both nodes congruent, we can probably do something */
2473 node->type.tv = tarval_bottom;
2478 * (Re-)compute the type for a Min.
2480 * @param node the node
2482 static void compute_Min(node_t *node) {
2483 ir_node *op = node->node;
2484 node_t *l = get_irn_node(get_binop_left(op));
2485 node_t *r = get_irn_node(get_binop_right(op));
2486 lattice_elem_t a = l->type;
2487 lattice_elem_t b = r->type;
2489 if (a.tv == tarval_top || b.tv == tarval_top) {
2490 node->type.tv = tarval_top;
2491 } else if (is_con(a) && is_con(b)) {
2492 /* both nodes are constants, we can probably do something */
2494 /* this case handles SymConsts as well */
2497 ir_mode *mode = get_irn_mode(op);
2498 tarval *tv_max = get_mode_max(mode);
2502 else if (b.tv == tv_max)
2504 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2505 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2506 node->type.tv = a.tv;
2508 node->type.tv = b.tv;
2510 node->type.tv = tarval_bad;
2513 } else if (r->part == l->part) {
2514 /* both nodes congruent, we can probably do something */
2517 node->type.tv = tarval_bottom;
2522 * (Re-)compute the type for a given node.
2524 * @param node the node
2526 static void compute(node_t *node) {
2527 ir_node *irn = node->node;
2530 #ifndef VERIFY_MONOTONE
2532 * Once a node reaches bottom, the type cannot fall further
2533 * in the lattice and we can stop computation.
2534 * Do not take this exit if the monotony verifier is
2535 * enabled to catch errors.
2537 if (node->type.tv == tarval_bottom)
2541 if (is_no_Block(irn)) {
2542 /* for pinned nodes, check its control input */
2543 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2544 node_t *block = get_irn_node(get_nodes_block(irn));
2546 if (block->type.tv == tarval_unreachable) {
2547 node->type.tv = tarval_top;
2553 func = (compute_func)node->node->op->ops.generic;
2559 * Identity functions: Note that one might thing that identity() is just a
2560 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2561 * here, because it expects that the identity node is one of the inputs, which is NOT
2562 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2563 * So, we have our own implementation, which copies some parts of equivalent_node()
2567 * Calculates the Identity for Phi nodes
2569 static node_t *identity_Phi(node_t *node) {
2570 ir_node *phi = node->node;
2571 ir_node *block = get_nodes_block(phi);
2572 node_t *n_part = NULL;
2575 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2576 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2578 if (pred_X->type.tv == tarval_reachable) {
2579 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2583 else if (n_part->part != pred->part) {
2584 /* incongruent inputs, not a follower */
2589 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2590 * tarval_top, is in the TOP partition and should NOT being split! */
2591 assert(n_part != NULL);
2593 } /* identity_Phi */
2596 * Calculates the Identity for commutative 0 neutral nodes.
2598 static node_t *identity_comm_zero_binop(node_t *node) {
2599 ir_node *op = node->node;
2600 node_t *a = get_irn_node(get_binop_left(op));
2601 node_t *b = get_irn_node(get_binop_right(op));
2602 ir_mode *mode = get_irn_mode(op);
2605 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2606 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2609 /* node: no input should be tarval_top, else the binop would be also
2610 * Top and not being split. */
2611 zero = get_mode_null(mode);
2612 if (a->type.tv == zero)
2614 if (b->type.tv == zero)
2617 } /* identity_comm_zero_binop */
2620 * Calculates the Identity for Shift nodes.
2622 static node_t *identity_shift(node_t *node) {
2623 ir_node *op = node->node;
2624 node_t *b = get_irn_node(get_binop_right(op));
2625 ir_mode *mode = get_irn_mode(b->node);
2628 /* node: no input should be tarval_top, else the binop would be also
2629 * Top and not being split. */
2630 zero = get_mode_null(mode);
2631 if (b->type.tv == zero)
2632 return get_irn_node(get_binop_left(op));
2634 } /* identity_shift */
2637 * Calculates the Identity for Mul nodes.
2639 static node_t *identity_Mul(node_t *node) {
2640 ir_node *op = node->node;
2641 node_t *a = get_irn_node(get_Mul_left(op));
2642 node_t *b = get_irn_node(get_Mul_right(op));
2643 ir_mode *mode = get_irn_mode(op);
2646 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2647 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2650 /* node: no input should be tarval_top, else the binop would be also
2651 * Top and not being split. */
2652 one = get_mode_one(mode);
2653 if (a->type.tv == one)
2655 if (b->type.tv == one)
2658 } /* identity_Mul */
2661 * Calculates the Identity for Sub nodes.
2663 static node_t *identity_Sub(node_t *node) {
2664 ir_node *sub = node->node;
2665 node_t *b = get_irn_node(get_Sub_right(sub));
2666 ir_mode *mode = get_irn_mode(sub);
2668 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2669 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2672 /* node: no input should be tarval_top, else the binop would be also
2673 * Top and not being split. */
2674 if (b->type.tv == get_mode_null(mode))
2675 return get_irn_node(get_Sub_left(sub));
2677 } /* identity_Sub */
2680 * Calculates the Identity for And nodes.
2682 static node_t *identity_And(node_t *node) {
2683 ir_node *and = node->node;
2684 node_t *a = get_irn_node(get_And_left(and));
2685 node_t *b = get_irn_node(get_And_right(and));
2686 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2688 /* node: no input should be tarval_top, else the And would be also
2689 * Top and not being split. */
2690 if (a->type.tv == neutral)
2692 if (b->type.tv == neutral)
2695 } /* identity_And */
2698 * Calculates the Identity for Confirm nodes.
2700 static node_t *identity_Confirm(node_t *node) {
2701 ir_node *confirm = node->node;
2703 /* a Confirm is always a Copy */
2704 return get_irn_node(get_Confirm_value(confirm));
2705 } /* identity_Confirm */
2708 * Calculates the Identity for Mux nodes.
2710 static node_t *identity_Mux(node_t *node) {
2711 ir_node *mux = node->node;
2712 node_t *t = get_irn_node(get_Mux_true(mux));
2713 node_t *f = get_irn_node(get_Mux_false(mux));
2716 if (t->part == f->part)
2719 /* for now, the 1-input identity is not supported */
2721 sel = get_irn_node(get_Mux_sel(mux));
2723 /* Mux sel input is mode_b, so it is always a tarval */
2724 if (sel->type.tv == tarval_b_true)
2726 if (sel->type.tv == tarval_b_false)
2730 } /* identity_Mux */
2733 * Calculates the Identity for Min nodes.
2735 static node_t *identity_Min(node_t *node) {
2736 ir_node *op = node->node;
2737 node_t *a = get_irn_node(get_binop_left(op));
2738 node_t *b = get_irn_node(get_binop_right(op));
2739 ir_mode *mode = get_irn_mode(op);
2742 if (a->part == b->part) {
2743 /* leader of multiple predecessors */
2747 /* works even with NaN */
2748 tv_max = get_mode_max(mode);
2749 if (a->type.tv == tv_max)
2751 if (b->type.tv == tv_max)
2754 } /* identity_Min */
2757 * Calculates the Identity for Max nodes.
2759 static node_t *identity_Max(node_t *node) {
2760 ir_node *op = node->node;
2761 node_t *a = get_irn_node(get_binop_left(op));
2762 node_t *b = get_irn_node(get_binop_right(op));
2763 ir_mode *mode = get_irn_mode(op);
2766 if (a->part == b->part) {
2767 /* leader of multiple predecessors */
2771 /* works even with NaN */
2772 tv_min = get_mode_min(mode);
2773 if (a->type.tv == tv_min)
2775 if (b->type.tv == tv_min)
2778 } /* identity_Max */
2781 * Calculates the Identity for nodes.
2783 static node_t *identity(node_t *node) {
2784 ir_node *irn = node->node;
2786 switch (get_irn_opcode(irn)) {
2788 return identity_Phi(node);
2790 return identity_Mul(node);
2794 return identity_comm_zero_binop(node);
2799 return identity_shift(node);
2801 return identity_And(node);
2803 return identity_Sub(node);
2805 return identity_Confirm(node);
2807 return identity_Mux(node);
2809 return identity_Min(node);
2811 return identity_Max(node);
2818 * Node follower is a (new) follower of leader, segregate Leader
2821 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2822 ir_node *l = leader->node;
2823 int j, i, n = get_irn_n_outs(l);
2825 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2826 /* The leader edges must remain sorted, but follower edges can
2828 for (i = leader->n_followers + 1; i <= n; ++i) {
2829 if (l->out[i].use == follower) {
2830 ir_def_use_edge t = l->out[i];
2832 for (j = i - 1; j >= leader->n_followers + 1; --j)
2833 l->out[j + 1] = l->out[j];
2834 ++leader->n_followers;
2835 l->out[leader->n_followers] = t;
2839 } /* segregate_def_use_chain_1 */
2842 * Node follower is a (new) follower segregate its Leader
2845 * @param follower the follower IR node
2847 static void segregate_def_use_chain(const ir_node *follower) {
2850 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2851 node_t *pred = get_irn_node(get_irn_n(follower, i));
2853 segregate_def_use_chain_1(follower, pred);
2855 } /* segregate_def_use_chain */
2858 * Propagate constant evaluation.
2860 * @param env the environment
2862 static void propagate(environment_t *env) {
2865 lattice_elem_t old_type;
2867 unsigned n_fallen, old_type_was_T_or_C;
2870 while (env->cprop != NULL) {
2871 void *oldopcode = NULL;
2873 /* remove the first partition X from cprop */
2876 env->cprop = X->cprop_next;
2878 old_type_was_T_or_C = X->type_is_T_or_C;
2880 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2884 int cprop_empty = list_empty(&X->cprop);
2885 int cprop_X_empty = list_empty(&X->cprop_X);
2887 if (cprop_empty && cprop_X_empty) {
2888 /* both cprop lists are empty */
2892 /* remove the first Node x from X.cprop */
2894 /* Get a node from the cprop_X list only if
2895 * all data nodes are processed.
2896 * This ensures, that all inputs of the Cond
2897 * predecessor are processed if its type is still Top.
2899 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2901 x = list_entry(X->cprop.next, node_t, cprop_list);
2904 //assert(x->part == X);
2905 list_del(&x->cprop_list);
2908 if (x->is_follower && identity(x) == x) {
2909 /* check the opcode first */
2910 if (oldopcode == NULL) {
2911 oldopcode = lambda_opcode(get_first_node(X), env);
2913 if (oldopcode != lambda_opcode(x, env)) {
2914 if (x->on_fallen == 0) {
2915 /* different opcode -> x falls out of this partition */
2920 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2924 /* x will make the follower -> leader transition */
2925 follower_to_leader(x);
2928 /* compute a new type for x */
2930 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2932 if (x->type.tv != old_type.tv) {
2933 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2934 verify_type(old_type, x);
2936 if (x->on_fallen == 0) {
2937 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2938 not already on the list. */
2943 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2945 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2946 ir_node *succ = get_irn_out(x->node, i);
2947 node_t *y = get_irn_node(succ);
2949 /* Add y to y.partition.cprop. */
2950 add_to_cprop(y, env);
2955 if (n_fallen > 0 && n_fallen != X->n_leader) {
2956 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2957 Y = split(&X, fallen, env);
2959 * We have split out fallen node. The type of the result
2960 * partition is NOT set yet.
2962 Y->type_is_T_or_C = 0;
2966 /* remove the flags from the fallen list */
2967 for (x = fallen; x != NULL; x = x->next)
2970 if (old_type_was_T_or_C) {
2973 /* check if some nodes will make the leader -> follower transition */
2974 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2975 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2976 node_t *eq_node = identity(y);
2978 if (eq_node != y && eq_node->part == y->part) {
2979 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2980 /* move to Follower */
2982 list_del(&y->node_list);
2983 list_add_tail(&y->node_list, &Y->Follower);
2986 segregate_def_use_chain(y->node);
2996 * Get the leader for a given node from its congruence class.
2998 * @param irn the node
3000 static ir_node *get_leader(node_t *node) {
3001 partition_t *part = node->part;
3003 if (part->n_leader > 1 || node->is_follower) {
3004 if (node->is_follower) {
3005 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
3008 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
3010 return get_first_node(part)->node;
3016 * Returns non-zero if a mode_T node has only one reachable output.
3018 static int only_one_reachable_proj(ir_node *n) {
3021 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
3022 ir_node *proj = get_irn_out(n, i);
3025 /* skip non-control flow Proj's */
3026 if (get_irn_mode(proj) != mode_X)
3029 node = get_irn_node(proj);
3030 if (node->type.tv == tarval_reachable) {
3036 } /* only_one_reachable_proj */
3039 * Return non-zero if the control flow predecessor node pred
3040 * is the only reachable control flow exit of its block.
3042 * @param pred the control flow exit
3044 static int can_exchange(ir_node *pred) {
3047 else if (is_Jmp(pred))
3049 else if (get_irn_mode(pred) == mode_T) {
3050 /* if the predecessor block has more than one
3051 reachable outputs we cannot remove the block */
3052 return only_one_reachable_proj(pred);
3055 } /* can_exchange */
3058 * Block Post-Walker, apply the analysis results on control flow by
3059 * shortening Phi's and Block inputs.
3061 static void apply_cf(ir_node *block, void *ctx) {
3062 environment_t *env = ctx;
3063 node_t *node = get_irn_node(block);
3065 ir_node **ins, **in_X;
3066 ir_node *phi, *next;
3068 n = get_Block_n_cfgpreds(block);
3070 if (node->type.tv == tarval_unreachable) {
3073 for (i = n - 1; i >= 0; --i) {
3074 ir_node *pred = get_Block_cfgpred(block, i);
3076 if (! is_Bad(pred)) {
3077 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3079 if (pred_bl->flagged == 0) {
3080 pred_bl->flagged = 3;
3082 if (pred_bl->type.tv == tarval_reachable) {
3084 * We will remove an edge from block to its pred.
3085 * This might leave the pred block as an endless loop
3087 if (! is_backedge(block, i))
3088 keep_alive(pred_bl->node);
3094 /* the EndBlock is always reachable even if the analysis
3095 finds out the opposite :-) */
3096 if (block != get_irg_end_block(current_ir_graph)) {
3097 /* mark dead blocks */
3098 set_Block_dead(block);
3099 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3101 /* the endblock is unreachable */
3102 set_irn_in(block, 0, NULL);
3108 /* only one predecessor combine */
3109 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3111 if (can_exchange(pred)) {
3112 ir_node *new_block = get_nodes_block(pred);
3113 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3114 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3115 exchange(block, new_block);
3116 node->node = new_block;
3122 NEW_ARR_A(ir_node *, in_X, n);
3124 for (i = 0; i < n; ++i) {
3125 ir_node *pred = get_Block_cfgpred(block, i);
3126 node_t *node = get_irn_node(pred);
3128 if (node->type.tv == tarval_reachable) {
3131 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3132 if (! is_Bad(pred)) {
3133 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3135 if (pred_bl->flagged == 0) {
3136 pred_bl->flagged = 3;
3138 if (pred_bl->type.tv == tarval_reachable) {
3140 * We will remove an edge from block to its pred.
3141 * This might leave the pred block as an endless loop
3143 if (! is_backedge(block, i))
3144 keep_alive(pred_bl->node);
3154 NEW_ARR_A(ir_node *, ins, n);
3155 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3156 node_t *node = get_irn_node(phi);
3158 next = get_Phi_next(phi);
3159 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3160 /* this Phi is replaced by a constant */
3161 tarval *tv = node->type.tv;
3162 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
3164 set_irn_node(c, node);
3166 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3167 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3172 for (i = 0; i < n; ++i) {
3173 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3175 if (pred->type.tv == tarval_reachable) {
3176 ins[j++] = get_Phi_pred(phi, i);
3180 /* this Phi is replaced by a single predecessor */
3181 ir_node *s = ins[0];
3182 node_t *phi_node = get_irn_node(phi);
3185 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3186 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3191 set_irn_in(phi, j, ins);
3199 /* this Block has only one live predecessor */
3200 ir_node *pred = skip_Proj(in_X[0]);
3202 if (can_exchange(pred)) {
3203 ir_node *new_block = get_nodes_block(pred);
3204 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3205 exchange(block, new_block);
3206 node->node = new_block;
3211 set_irn_in(block, k, in_X);
3216 * Exchange a node by its leader.
3217 * Beware: in rare cases the mode might be wrong here, for instance
3218 * AddP(x, NULL) is a follower of x, but with different mode.
3221 static void exchange_leader(ir_node *irn, ir_node *leader) {
3222 ir_mode *mode = get_irn_mode(irn);
3223 if (mode != get_irn_mode(leader)) {
3224 /* The conv is a no-op, so we are free to place it
3225 * either in the block of the leader OR in irn's block.
3226 * Probably placing it into leaders block might reduce
3227 * the number of Conv due to CSE. */
3228 ir_node *block = get_nodes_block(leader);
3229 dbg_info *dbg = get_irn_dbg_info(irn);
3231 leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
3233 exchange(irn, leader);
3234 } /* exchange_leader */
3237 * Check, if all users of a mode_M node are dead. Use
3238 * the Def-Use edges for this purpose, as they still
3239 * reflect the situation.
3241 static int all_users_are_dead(const ir_node *irn) {
3242 int i, n = get_irn_n_outs(irn);
3244 for (i = 1; i <= n; ++i) {
3245 const ir_node *succ = irn->out[i].use;
3246 const ir_node *block = get_nodes_block(succ);
3247 const node_t *bl = get_irn_node(block);
3250 if (bl->type.tv == tarval_unreachable) {
3251 /* block is unreachable */
3254 node = get_irn_node(succ);
3255 if (node->type.tv != tarval_top) {
3256 /* found a reachable user */
3260 /* all users are unreachable */
3262 } /* all_user_are_dead */
3265 * Walker: Find reachable mode_M nose that have only
3266 * unreachable users. These nodes must be kept later.
3268 static void find_kept_memory(ir_node *irn, void *ctx) {
3269 environment_t *env = ctx;
3270 node_t *node, *block;
3272 if (is_Block(irn)) {
3275 if (get_irn_mode(irn) != mode_M)
3278 block = get_irn_node(get_nodes_block(irn));
3279 if (block->type.tv == tarval_unreachable)
3282 node = get_irn_node(irn);
3283 if (node->type.tv == tarval_top)
3286 /* ok, we found a live memory node. */
3287 if (all_users_are_dead(irn)) {
3288 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3289 ARR_APP1(ir_node *, env->kept_memory, irn);
3291 } /* find_kept_memory */
3294 * Post-Walker, apply the analysis results;
3296 static void apply_result(ir_node *irn, void *ctx) {
3297 environment_t *env = ctx;
3298 node_t *node = get_irn_node(irn);
3300 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3301 /* blocks already handled, do not touch the End node */
3303 node_t *block = get_irn_node(get_nodes_block(irn));
3305 if (block->type.tv == tarval_unreachable) {
3306 ir_node *bad = get_irg_bad(current_ir_graph);
3308 /* here, bad might already have a node, but this can be safely ignored
3309 as long as bad has at least ONE valid node */
3310 set_irn_node(bad, node);
3312 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3315 } else if (node->type.tv == tarval_top) {
3316 ir_mode *mode = get_irn_mode(irn);
3318 if (mode == mode_M) {
3319 /* never kill a mode_M node */
3321 ir_node *pred = get_Proj_pred(irn);
3322 node_t *pnode = get_irn_node(pred);
3324 if (pnode->type.tv == tarval_top) {
3325 /* skip the predecessor */
3326 ir_node *mem = get_memop_mem(pred);
3328 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3333 /* leave other nodes, especially PhiM */
3334 } else if (mode == mode_T) {
3335 /* Do not kill mode_T nodes, kill their Projs */
3336 } else if (! is_Unknown(irn)) {
3337 /* don't kick away Unknown's, they might be still needed */
3338 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3340 /* control flow should already be handled at apply_cf() */
3341 assert(mode != mode_X);
3343 /* see comment above */
3344 set_irn_node(unk, node);
3346 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3351 else if (get_irn_mode(irn) == mode_X) {
3354 ir_node *cond = get_Proj_pred(irn);
3356 if (is_Cond(cond)) {
3357 if (only_one_reachable_proj(cond)) {
3358 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
3359 set_irn_node(jmp, node);
3361 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3362 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3366 node_t *sel = get_irn_node(get_Cond_selector(cond));
3367 tarval *tv = sel->type.tv;
3369 if (is_tarval(tv) && tarval_is_constant(tv)) {
3370 /* The selector is a constant, but more
3371 * than one output is active: An unoptimized
3379 /* normal data node */
3380 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3381 tarval *tv = node->type.tv;
3384 * Beware: never replace mode_T nodes by constants. Currently we must mark
3385 * mode_T nodes with constants, but do NOT replace them.
3387 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3388 /* can be replaced by a constant */
3389 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
3390 set_irn_node(c, node);
3392 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3393 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3394 exchange_leader(irn, c);
3397 } else if (is_entity(node->type.sym.entity_p)) {
3398 if (! is_SymConst(irn)) {
3399 /* can be replaced by a SymConst */
3400 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3401 set_irn_node(symc, node);
3404 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3405 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3406 exchange_leader(irn, symc);
3409 } else if (is_Confirm(irn)) {
3410 /* Confirms are always follower, but do not kill them here */
3412 ir_node *leader = get_leader(node);
3414 if (leader != irn) {
3415 int non_strict_phi = 0;
3418 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3419 * as this might create non-strict programs.
3421 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3424 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3425 ir_node *pred = get_Phi_pred(irn, i);
3427 if (is_Unknown(pred)) {
3433 if (! non_strict_phi) {
3434 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3435 if (node->is_follower)
3436 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3438 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3439 exchange_leader(irn, leader);
3446 } /* apply_result */
3449 * Fix the keep-alives by deleting unreachable ones.
3451 static void apply_end(ir_node *end, environment_t *env) {
3452 int i, j, n = get_End_n_keepalives(end);
3456 NEW_ARR_A(ir_node *, in, n);
3458 /* fix the keep alive */
3459 for (i = j = 0; i < n; i++) {
3460 ir_node *ka = get_End_keepalive(end, i);
3461 node_t *node = get_irn_node(ka);
3464 node = get_irn_node(get_nodes_block(ka));
3466 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3470 set_End_keepalives(end, j, in);
3475 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3478 * sets the generic functions to compute.
3480 static void set_compute_functions(void) {
3483 /* set the default compute function */
3484 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3485 ir_op *op = get_irp_opcode(i);
3486 op->ops.generic = (op_func)default_compute;
3489 /* set specific functions */
3510 } /* set_compute_functions */
3515 static void add_memory_keeps(ir_node **kept_memory, int len) {
3516 ir_node *end = get_irg_end(current_ir_graph);
3520 ir_nodeset_init(&set);
3522 /* check, if those nodes are already kept */
3523 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3524 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3526 for (i = len - 1; i >= 0; --i) {
3527 ir_node *ka = kept_memory[i];
3529 if (! ir_nodeset_contains(&set, ka)) {
3530 add_End_keepalive(end, ka);
3533 ir_nodeset_destroy(&set);
3534 } /* add_memory_keeps */
3536 void combo(ir_graph *irg) {
3538 ir_node *initial_bl;
3540 ir_graph *rem = current_ir_graph;
3543 current_ir_graph = irg;
3545 /* register a debug mask */
3546 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3548 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3550 obstack_init(&env.obst);
3551 env.worklist = NULL;
3555 #ifdef DEBUG_libfirm
3556 env.dbg_list = NULL;
3558 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3559 env.type2id_map = pmap_create();
3560 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3561 env.end_idx = get_opt_global_cse() ? 0 : -1;
3562 env.lambda_input = 0;
3565 /* options driving the optimization */
3566 env.commutative = 1;
3567 env.opt_unknown = 1;
3569 assure_irg_outs(irg);
3570 assure_cf_loop(irg);
3572 /* we have our own value_of function */
3573 set_value_of_func(get_node_tarval);
3575 set_compute_functions();
3576 DEBUG_ONLY(part_nr = 0);
3578 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3580 if (env.opt_unknown)
3581 tarval_UNKNOWN = tarval_top;
3583 tarval_UNKNOWN = tarval_bad;
3585 /* create the initial partition and place it on the work list */
3586 env.initial = new_partition(&env);
3587 add_to_worklist(env.initial, &env);
3588 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3590 /* set the hook: from now, every node has a partition and a type */
3591 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3593 /* all nodes on the initial partition have type Top */
3594 env.initial->type_is_T_or_C = 1;
3596 /* Place the START Node's partition on cprop.
3597 Place the START Node on its local worklist. */
3598 initial_bl = get_irg_start_block(irg);
3599 start = get_irn_node(initial_bl);
3600 add_to_cprop(start, &env);
3604 if (env.worklist != NULL)
3606 } while (env.cprop != NULL || env.worklist != NULL);
3608 dump_all_partitions(&env);
3609 check_all_partitions(&env);
3612 dump_ir_block_graph(irg, "-partition");
3615 /* apply the result */
3617 /* check, which nodes must be kept */
3618 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3620 /* kill unreachable control flow */
3621 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3622 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3623 * and fixes assertion because dead cf to dead blocks is NOT removed by
3625 apply_end(get_irg_end(irg), &env);
3626 irg_walk_graph(irg, NULL, apply_result, &env);
3628 len = ARR_LEN(env.kept_memory);
3630 add_memory_keeps(env.kept_memory, len);
3633 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3637 /* control flow might changed */
3638 set_irg_outs_inconsistent(irg);
3639 set_irg_extblk_inconsistent(irg);
3640 set_irg_doms_inconsistent(irg);
3641 set_irg_loopinfo_inconsistent(irg);
3644 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3646 /* remove the partition hook */
3647 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3649 DEL_ARR_F(env.kept_memory);
3650 pmap_destroy(env.type2id_map);
3651 del_set(env.opcode2id_map);
3652 obstack_free(&env.obst, NULL);
3654 /* restore value_of() default behavior */
3655 set_value_of_func(NULL);
3656 current_ir_graph = rem;