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 * - support for global congruences is implemented but not tested yet
33 * Note further that we use the terminology from Click's work here, which is different
34 * in some cases from Firm terminology. Especially, Click's type is a
35 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
41 #include "iroptimize.h"
49 #include "irgraph_t.h"
56 #include "iropt_dbg.h"
66 /* define this to check that all type translations are monotone */
67 #undef VERIFY_MONOTONE
69 /* define this to check the consistency of partitions */
70 #define CHECK_PARTITIONS
72 typedef struct node_t node_t;
73 typedef struct partition_t partition_t;
74 typedef struct opcode_key_t opcode_key_t;
75 typedef struct listmap_entry_t listmap_entry_t;
77 /** The type of the compute function. */
78 typedef void (*compute_func)(node_t *node);
84 ir_opcode code; /**< The Firm opcode. */
85 ir_mode *mode; /**< The mode of all nodes in the partition. */
86 int arity; /**< The arity of this opcode (needed for Phi etc. */
88 long proj; /**< For Proj nodes, its proj number */
89 ir_entity *ent; /**< For Sel Nodes, its entity */
94 * An entry in the list_map.
96 struct listmap_entry_t {
97 void *id; /**< The id. */
98 node_t *list; /**< The associated list for this id. */
99 listmap_entry_t *next; /**< Link to the next entry in the map. */
102 /** We must map id's to lists. */
103 typedef struct listmap_t {
104 set *map; /**< Map id's to listmap_entry_t's */
105 listmap_entry_t *values; /**< List of all values in the map. */
109 * A lattice element. Because we handle constants and symbolic constants different, we
110 * have to use this union.
121 ir_node *node; /**< The IR-node itself. */
122 list_head node_list; /**< Double-linked list of leader/follower entries. */
123 list_head cprop_list; /**< Double-linked partition.cprop list. */
124 partition_t *part; /**< points to the partition this node belongs to */
125 node_t *next; /**< Next node on local list (partition.touched, fallen). */
126 node_t *race_next; /**< Next node on race list. */
127 lattice_elem_t type; /**< The associated lattice element "type". */
128 int max_user_input; /**< Maximum input number of Def-Use edges. */
129 int next_edge; /**< Index of the next Def-Use edge to use. */
130 int n_followers; /**< Number of Follower in the outs set. */
131 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
132 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
133 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
134 unsigned is_follower:1; /**< Set, if this node is a follower. */
135 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
136 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
140 * A partition containing congruent nodes.
143 list_head Leader; /**< The head of partition Leader node list. */
144 list_head Follower; /**< The head of partition Follower node list. */
145 list_head cprop; /**< The head of partition.cprop list. */
146 partition_t *wl_next; /**< Next entry in the work list if any. */
147 partition_t *touched_next; /**< Points to the next partition in the touched set. */
148 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
149 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
150 node_t *touched; /**< The partition.touched set of this partition. */
151 unsigned n_leader; /**< Number of entries in this partition.Leader. */
152 unsigned n_touched; /**< Number of entries in the partition.touched. */
153 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
154 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
155 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
156 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
157 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
159 partition_t *dbg_next; /**< Link all partitions for debugging */
160 unsigned nr; /**< A unique number for (what-)mapping, >0. */
164 typedef struct environment_t {
165 struct obstack obst; /**< obstack to allocate data structures. */
166 partition_t *worklist; /**< The work list. */
167 partition_t *cprop; /**< The constant propagation list. */
168 partition_t *touched; /**< the touched set. */
169 partition_t *initial; /**< The initial partition. */
170 set *opcode2id_map; /**< The opcodeMode->id map. */
171 pmap *type2id_map; /**< The type->id map. */
172 int end_idx; /**< -1 for local and 0 for global congruences. */
173 int lambda_input; /**< Captured argument for lambda_partition(). */
174 char nonstd_cond; /**< Set, if a Condb note has a non-Cmp predecessor. */
175 char modified; /**< Set, if the graph was modified. */
176 char commutative; /**< Set, if commutation nodes should be handled specially. */
178 partition_t *dbg_list; /**< List of all partitions. */
182 /** Type of the what function. */
183 typedef void *(*what_func)(const node_t *node, environment_t *env);
185 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
186 #define set_irn_node(follower, node) set_irn_link(follower, node)
188 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
189 #undef tarval_unreachable
190 #define tarval_unreachable tarval_top
193 /** The debug module handle. */
194 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
196 /** The what reason. */
197 DEBUG_ONLY(static const char *what_reason;)
199 /** Next partition number. */
200 DEBUG_ONLY(static unsigned part_nr = 0);
202 /** The tarval returned by Unknown nodes. */
203 static tarval *tarval_UNKNOWN;
206 static node_t *identity(node_t *node);
208 #ifdef CHECK_PARTITIONS
212 static void check_partition(const partition_t *T) {
216 list_for_each_entry(node_t, node, &T->Leader, node_list) {
217 assert(node->is_follower == 0);
218 assert(node->flagged == 0);
219 assert(node->part == T);
222 assert(n == T->n_leader);
224 list_for_each_entry(node_t, node, &T->Follower, node_list) {
225 assert(node->is_follower == 1);
226 assert(node->flagged == 0);
227 assert(node->part == T);
229 } /* check_partition */
232 * check that all leader nodes in the partition have the same opcode.
234 static void check_opcode(const partition_t *Z) {
239 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
240 ir_node *irn = node->node;
243 key.code = get_irn_opcode(irn);
244 key.mode = get_irn_mode(irn);
245 key.arity = get_irn_arity(irn);
249 switch (get_irn_opcode(irn)) {
251 key.u.proj = get_Proj_proj(irn);
254 key.u.ent = get_Sel_entity(irn);
261 assert(key.code == get_irn_opcode(irn));
262 assert(key.mode == get_irn_mode(irn));
263 assert(key.arity == get_irn_arity(irn));
265 switch (get_irn_opcode(irn)) {
267 assert(key.u.proj == get_Proj_proj(irn));
270 assert(key.u.ent == get_Sel_entity(irn));
279 static void check_all_partitions(environment_t *env) {
284 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
286 if (! P->type_is_T_or_C)
288 list_for_each_entry(node_t, node, &P->Follower, node_list) {
289 node_t *leader = identity(node);
291 assert(leader != node && leader->part == node->part);
300 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
303 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
304 for (e = list; e != NULL; e = NEXT(e)) {
305 assert(e->part == Z);
308 } /* ido_check_list */
311 * Check a local list.
313 static void check_list(const node_t *list, const partition_t *Z) {
314 do_check_list(list, offsetof(node_t, next), Z);
318 #define check_partition(T)
319 #define check_list(list, Z)
320 #define check_all_partitions(env)
321 #endif /* CHECK_PARTITIONS */
324 static inline lattice_elem_t get_partition_type(const partition_t *X);
327 * Dump partition to output.
329 static void dump_partition(const char *msg, const partition_t *part) {
332 lattice_elem_t type = get_partition_type(part);
334 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
335 msg, part->nr, part->type_is_T_or_C ? "*" : "",
336 part->n_leader, type));
337 list_for_each_entry(node_t, node, &part->Leader, node_list) {
338 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
341 if (! list_empty(&part->Follower)) {
342 DB((dbg, LEVEL_2, "\n---\n "));
344 list_for_each_entry(node_t, node, &part->Follower, node_list) {
345 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
349 DB((dbg, LEVEL_2, "\n}\n"));
350 } /* dump_partition */
355 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
359 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
361 DB((dbg, LEVEL_3, "%s = {\n ", msg));
362 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
363 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
366 DB((dbg, LEVEL_3, "\n}\n"));
374 static void dump_race_list(const char *msg, const node_t *list) {
375 do_dump_list(msg, list, offsetof(node_t, race_next));
376 } /* dump_race_list */
379 * Dumps a local list.
381 static void dump_list(const char *msg, const node_t *list) {
382 do_dump_list(msg, list, offsetof(node_t, next));
386 * Dump all partitions.
388 static void dump_all_partitions(const environment_t *env) {
389 const partition_t *P;
391 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
392 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
393 dump_partition("", P);
394 } /* dump_all_partitions */
399 static void dump_split_list(const partition_t *list) {
400 const partition_t *p;
402 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
403 for (p = list; p != NULL; p = p->split_next)
404 DB((dbg, LEVEL_2, "part%u, ", p->nr));
405 DB((dbg, LEVEL_2, "\n}\n"));
406 } /* dump_split_list */
409 #define dump_partition(msg, part)
410 #define dump_race_list(msg, list)
411 #define dump_list(msg, list)
412 #define dump_all_partitions(env)
413 #define dump_split_list(list)
416 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
418 * Verify that a type transition is monotone
420 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
421 if (old_type.tv == new_type.tv) {
425 if (old_type.tv == tarval_top) {
426 /* from Top down-to is always allowed */
429 if (old_type.tv == tarval_reachable) {
430 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
432 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
436 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
439 #define verify_type(old_type, new_type)
443 * Compare two pointer values of a listmap.
445 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
446 const listmap_entry_t *e1 = elt;
447 const listmap_entry_t *e2 = key;
450 return e1->id != e2->id;
451 } /* listmap_cmp_ptr */
454 * Initializes a listmap.
456 * @param map the listmap
458 static void listmap_init(listmap_t *map) {
459 map->map = new_set(listmap_cmp_ptr, 16);
464 * Terminates a listmap.
466 * @param map the listmap
468 static void listmap_term(listmap_t *map) {
473 * Return the associated listmap entry for a given id.
475 * @param map the listmap
476 * @param id the id to search for
478 * @return the associated listmap entry for the given id
480 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
481 listmap_entry_t key, *entry;
486 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
488 if (entry->list == NULL) {
489 /* a new entry, put into the list */
490 entry->next = map->values;
497 * Calculate the hash value for an opcode map entry.
499 * @param entry an opcode map entry
501 * @return a hash value for the given opcode map entry
503 static unsigned opcode_hash(const opcode_key_t *entry) {
504 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
508 * Compare two entries in the opcode map.
510 static int cmp_opcode(const void *elt, const void *key, size_t size) {
511 const opcode_key_t *o1 = elt;
512 const opcode_key_t *o2 = key;
515 return o1->code != o2->code || o1->mode != o2->mode ||
516 o1->arity != o2->arity ||
517 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
521 * Compare two Def-Use edges for input position.
523 static int cmp_def_use_edge(const void *a, const void *b) {
524 const ir_def_use_edge *ea = a;
525 const ir_def_use_edge *eb = b;
527 /* no overrun, because range is [-1, MAXINT] */
528 return ea->pos - eb->pos;
529 } /* cmp_def_use_edge */
532 * We need the Def-Use edges sorted.
534 static void sort_irn_outs(node_t *node) {
535 ir_node *irn = node->node;
536 int n_outs = get_irn_n_outs(irn);
539 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
541 node->max_user_input = irn->out[n_outs].pos;
542 } /* sort_irn_outs */
545 * Return the type of a node.
547 * @param irn an IR-node
549 * @return the associated type of this node
551 static inline lattice_elem_t get_node_type(const ir_node *irn) {
552 return get_irn_node(irn)->type;
553 } /* get_node_type */
556 * Return the tarval of a node.
558 * @param irn an IR-node
560 * @return the associated type of this node
562 static inline tarval *get_node_tarval(const ir_node *irn) {
563 lattice_elem_t type = get_node_type(irn);
565 if (is_tarval(type.tv))
567 return tarval_bottom;
568 } /* get_node_type */
571 * Add a partition to the worklist.
573 static inline void add_to_worklist(partition_t *X, environment_t *env) {
574 assert(X->on_worklist == 0);
575 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
576 X->wl_next = env->worklist;
579 } /* add_to_worklist */
582 * Create a new empty partition.
584 * @param env the environment
586 * @return a newly allocated partition
588 static inline partition_t *new_partition(environment_t *env) {
589 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
591 INIT_LIST_HEAD(&part->Leader);
592 INIT_LIST_HEAD(&part->Follower);
593 INIT_LIST_HEAD(&part->cprop);
594 part->wl_next = NULL;
595 part->touched_next = NULL;
596 part->cprop_next = NULL;
597 part->split_next = NULL;
598 part->touched = NULL;
601 part->max_user_inputs = 0;
602 part->on_worklist = 0;
603 part->on_touched = 0;
605 part->type_is_T_or_C = 0;
607 part->dbg_next = env->dbg_list;
608 env->dbg_list = part;
609 part->nr = part_nr++;
613 } /* new_partition */
616 * Get the first node from a partition.
618 static inline node_t *get_first_node(const partition_t *X) {
619 return list_entry(X->Leader.next, node_t, node_list);
620 } /* get_first_node */
623 * Return the type of a partition (assuming partition is non-empty and
624 * all elements have the same type).
626 * @param X a partition
628 * @return the type of the first element of the partition
630 static inline lattice_elem_t get_partition_type(const partition_t *X) {
631 const node_t *first = get_first_node(X);
633 } /* get_partition_type */
636 * Creates a partition node for the given IR-node and place it
637 * into the given partition.
639 * @param irn an IR-node
640 * @param part a partition to place the node in
641 * @param env the environment
643 * @return the created node
645 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
646 /* create a partition node and place it in the partition */
647 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
649 INIT_LIST_HEAD(&node->node_list);
650 INIT_LIST_HEAD(&node->cprop_list);
654 node->race_next = NULL;
655 node->type.tv = tarval_top;
656 node->max_user_input = 0;
658 node->n_followers = 0;
659 node->on_touched = 0;
662 node->is_follower = 0;
663 node->by_all_const = 0;
665 set_irn_node(irn, node);
667 list_add_tail(&node->node_list, &part->Leader);
671 } /* create_partition_node */
674 * Pre-Walker, init all Block-Phi lists.
676 static void init_block_phis(ir_node *irn, void *env) {
680 set_Block_phis(irn, NULL);
682 } /* init_block_phis */
685 * Post-Walker, initialize all Nodes' type to U or top and place
686 * all nodes into the TOP partition.
688 static void create_initial_partitions(ir_node *irn, void *ctx) {
689 environment_t *env = ctx;
690 partition_t *part = env->initial;
693 node = create_partition_node(irn, part, env);
695 if (node->max_user_input > part->max_user_inputs)
696 part->max_user_inputs = node->max_user_input;
699 add_Block_phi(get_nodes_block(irn), irn);
700 } else if (is_Cond(irn)) {
701 /* check if all Cond's have a Cmp predecessor. */
702 if (get_irn_mode(irn) == mode_b && !is_Cmp(skip_Proj(get_Cond_selector(irn))))
703 env->nonstd_cond = 1;
705 } /* create_initial_partitions */
708 * Add a node to the entry.partition.touched set and
709 * node->partition to the touched set if not already there.
712 * @param env the environment
714 static inline void add_to_touched(node_t *y, environment_t *env) {
715 if (y->on_touched == 0) {
716 partition_t *part = y->part;
718 y->next = part->touched;
723 if (part->on_touched == 0) {
724 part->touched_next = env->touched;
726 part->on_touched = 1;
729 check_list(part->touched, part);
731 } /* add_to_touched */
734 * Place a node on the cprop list.
737 * @param env the environment
739 static void add_to_cprop(node_t *y, environment_t *env) {
740 /* Add y to y.partition.cprop. */
741 if (y->on_cprop == 0) {
742 partition_t *Y = y->part;
744 list_add_tail(&y->cprop_list, &Y->cprop);
747 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
749 /* place its partition on the cprop list */
750 if (Y->on_cprop == 0) {
751 Y->cprop_next = env->cprop;
756 if (get_irn_mode(y->node) == mode_T) {
757 /* mode_T nodes always produce tarval_bottom, so we must explicitly
758 add it's Proj's to get constant evaluation to work */
761 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
762 node_t *proj = get_irn_node(get_irn_out(y->node, i));
764 add_to_cprop(proj, env);
766 } else if (is_Block(y->node)) {
767 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
768 * if someone placed the block. The Block is only placed if the reachability
769 * changes, and this must be re-evaluated in compute_Phi(). */
771 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
772 node_t *p = get_irn_node(phi);
773 add_to_cprop(p, env);
779 * Update the worklist: If Z is on worklist then add Z' to worklist.
780 * Else add the smaller of Z and Z' to worklist.
782 * @param Z the Z partition
783 * @param Z_prime the Z' partition, a previous part of Z
784 * @param env the environment
786 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
787 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
788 add_to_worklist(Z_prime, env);
790 add_to_worklist(Z, env);
792 } /* update_worklist */
795 * Make all inputs to x no longer be F.def_use edges.
799 static void move_edges_to_leader(node_t *x) {
800 ir_node *irn = x->node;
803 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
804 node_t *pred = get_irn_node(get_irn_n(irn, i));
809 n = get_irn_n_outs(p);
810 for (j = 1; j <= pred->n_followers; ++j) {
811 if (p->out[j].pos == i && p->out[j].use == irn) {
812 /* found a follower edge to x, move it to the Leader */
813 ir_def_use_edge edge = p->out[j];
815 /* remove this edge from the Follower set */
816 p->out[j] = p->out[pred->n_followers];
819 /* sort it into the leader set */
820 for (k = pred->n_followers + 2; k <= n; ++k) {
821 if (p->out[k].pos >= edge.pos)
823 p->out[k - 1] = p->out[k];
825 /* place the new edge here */
826 p->out[k - 1] = edge;
828 /* edge found and moved */
833 } /* move_edges_to_leader */
836 * Split a partition that has NO followers by a local list.
838 * @param Z partition to split
839 * @param g a (non-empty) node list
840 * @param env the environment
842 * @return a new partition containing the nodes of g
844 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
845 partition_t *Z_prime;
850 dump_partition("Splitting ", Z);
851 dump_list("by list ", g);
855 /* Remove g from Z. */
856 for (node = g; node != NULL; node = node->next) {
857 assert(node->part == Z);
858 list_del(&node->node_list);
861 assert(n < Z->n_leader);
864 /* Move g to a new partition, Z'. */
865 Z_prime = new_partition(env);
867 for (node = g; node != NULL; node = node->next) {
868 list_add_tail(&node->node_list, &Z_prime->Leader);
869 node->part = Z_prime;
870 if (node->max_user_input > max_input)
871 max_input = node->max_user_input;
873 Z_prime->max_user_inputs = max_input;
874 Z_prime->n_leader = n;
877 check_partition(Z_prime);
879 /* for now, copy the type info tag, it will be adjusted in split_by(). */
880 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
882 update_worklist(Z, Z_prime, env);
884 dump_partition("Now ", Z);
885 dump_partition("Created new ", Z_prime);
887 } /* split_no_followers */
890 * Make the Follower -> Leader transition for a node.
894 static void follower_to_leader(node_t *n) {
895 assert(n->is_follower == 1);
897 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
899 move_edges_to_leader(n);
900 list_del(&n->node_list);
901 list_add_tail(&n->node_list, &n->part->Leader);
903 } /* follower_to_leader */
906 * The environment for one race step.
908 typedef struct step_env {
909 node_t *initial; /**< The initial node list. */
910 node_t *unwalked; /**< The unwalked node list. */
911 node_t *walked; /**< The walked node list. */
912 int index; /**< Next index of Follower use_def edge. */
913 unsigned side; /**< side number. */
917 * Return non-zero, if a input is a real follower
919 * @param irn the node to check
920 * @param input number of the input
922 static int is_real_follower(const ir_node *irn, int input) {
925 switch (get_irn_opcode(irn)) {
928 /* ignore the Confirm bound input */
934 /* ignore the Mux sel input */
939 /* dead inputs are not follower edges */
940 ir_node *block = get_nodes_block(irn);
941 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
943 if (pred->type.tv == tarval_unreachable)
953 /* only a Sub x,0 / Shift x,0 might be a follower */
960 pred = get_irn_node(get_irn_n(irn, input));
961 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
965 pred = get_irn_node(get_irn_n(irn, input));
966 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
970 pred = get_irn_node(get_irn_n(irn, input));
971 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
976 /* all inputs are followers */
979 assert(!"opcode not implemented yet");
983 } /* is_real_follower */
986 * Do one step in the race.
988 static int step(step_env *env) {
991 if (env->initial != NULL) {
992 /* Move node from initial to unwalked */
994 env->initial = n->race_next;
996 n->race_next = env->unwalked;
1002 while (env->unwalked != NULL) {
1003 /* let n be the first node in unwalked */
1005 while (env->index < n->n_followers) {
1006 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1008 /* let m be n.F.def_use[index] */
1009 node_t *m = get_irn_node(edge->use);
1011 assert(m->is_follower);
1013 * Some inputs, like the get_Confirm_bound are NOT
1014 * real followers, sort them out.
1016 if (! is_real_follower(m->node, edge->pos)) {
1022 /* only followers from our partition */
1023 if (m->part != n->part)
1026 if ((m->flagged & env->side) == 0) {
1027 m->flagged |= env->side;
1029 if (m->flagged != 3) {
1030 /* visited the first time */
1031 /* add m to unwalked not as first node (we might still need to
1032 check for more follower node */
1033 m->race_next = n->race_next;
1037 /* else already visited by the other side and on the other list */
1040 /* move n to walked */
1041 env->unwalked = n->race_next;
1042 n->race_next = env->walked;
1050 * Clear the flags from a list and check for
1051 * nodes that where touched from both sides.
1053 * @param list the list
1055 static int clear_flags(node_t *list) {
1059 for (n = list; n != NULL; n = n->race_next) {
1060 if (n->flagged == 3) {
1061 /* we reach a follower from both sides, this will split congruent
1062 * inputs and make it a leader. */
1063 follower_to_leader(n);
1072 * Split a partition by a local list using the race.
1074 * @param pX pointer to the partition to split, might be changed!
1075 * @param gg a (non-empty) node list
1076 * @param env the environment
1078 * @return a new partition containing the nodes of gg
1080 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1081 partition_t *X = *pX;
1082 partition_t *X_prime;
1085 node_t *g, *h, *node, *t;
1086 int max_input, transitions, winner, shf;
1088 DEBUG_ONLY(static int run = 0;)
1090 DB((dbg, LEVEL_2, "Run %d ", run++));
1091 if (list_empty(&X->Follower)) {
1092 /* if the partition has NO follower, we can use the fast
1093 splitting algorithm. */
1094 return split_no_followers(X, gg, env);
1096 /* else do the race */
1098 dump_partition("Splitting ", X);
1099 dump_list("by list ", gg);
1101 INIT_LIST_HEAD(&tmp);
1103 /* Remove gg from X.Leader and put into g */
1105 for (node = gg; node != NULL; node = node->next) {
1106 assert(node->part == X);
1107 assert(node->is_follower == 0);
1109 list_del(&node->node_list);
1110 list_add_tail(&node->node_list, &tmp);
1111 node->race_next = g;
1116 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1117 node->race_next = h;
1120 /* restore X.Leader */
1121 list_splice(&tmp, &X->Leader);
1123 senv[0].initial = g;
1124 senv[0].unwalked = NULL;
1125 senv[0].walked = NULL;
1129 senv[1].initial = h;
1130 senv[1].unwalked = NULL;
1131 senv[1].walked = NULL;
1136 * Some informations on the race that are not stated clearly in Click's
1138 * 1) A follower stays on the side that reach him first.
1139 * 2) If the other side reches a follower, if will be converted to
1140 * a leader. /This must be done after the race is over, else the
1141 * edges we are iterating on are renumbered./
1142 * 3) /New leader might end up on both sides./
1143 * 4) /If one side ends up with new Leaders, we must ensure that
1144 * they can split out by opcode, hence we have to put _every_
1145 * partition with new Leader nodes on the cprop list, as
1146 * opcode splitting is done by split_by() at the end of
1147 * constant propagation./
1150 if (step(&senv[0])) {
1154 if (step(&senv[1])) {
1159 assert(senv[winner].initial == NULL);
1160 assert(senv[winner].unwalked == NULL);
1162 /* clear flags from walked/unwalked */
1164 transitions = clear_flags(senv[0].unwalked) << shf;
1165 transitions |= clear_flags(senv[0].walked) << shf;
1167 transitions |= clear_flags(senv[1].unwalked) << shf;
1168 transitions |= clear_flags(senv[1].walked) << shf;
1170 dump_race_list("winner ", senv[winner].walked);
1172 /* Move walked_{winner} to a new partition, X'. */
1173 X_prime = new_partition(env);
1176 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1177 list_del(&node->node_list);
1178 node->part = X_prime;
1179 if (node->is_follower) {
1180 list_add_tail(&node->node_list, &X_prime->Follower);
1182 list_add_tail(&node->node_list, &X_prime->Leader);
1185 if (node->max_user_input > max_input)
1186 max_input = node->max_user_input;
1188 X_prime->n_leader = n;
1189 X_prime->max_user_inputs = max_input;
1190 X->n_leader -= X_prime->n_leader;
1192 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1193 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1196 * Even if a follower was not checked by both sides, it might have
1197 * loose its congruence, so we need to check this case for all follower.
1199 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1200 if (identity(node) == node) {
1201 follower_to_leader(node);
1207 check_partition(X_prime);
1209 /* X' is the smaller part */
1210 add_to_worklist(X_prime, env);
1213 * If there where follower to leader transitions, ensure that the nodes
1214 * can be split out if necessary.
1216 if (transitions & 1) {
1217 /* place winner partition on the cprop list */
1218 if (X_prime->on_cprop == 0) {
1219 X_prime->cprop_next = env->cprop;
1220 env->cprop = X_prime;
1221 X_prime->on_cprop = 1;
1224 if (transitions & 2) {
1225 /* place other partition on the cprop list */
1226 if (X->on_cprop == 0) {
1227 X->cprop_next = env->cprop;
1233 dump_partition("Now ", X);
1234 dump_partition("Created new ", X_prime);
1236 /* we have to ensure that the partition containing g is returned */
1246 * Returns non-zero if the i'th input of a Phi node is live.
1248 * @param phi a Phi-node
1249 * @param i an input number
1251 * @return non-zero if the i'th input of the given Phi node is live
1253 static int is_live_input(ir_node *phi, int i) {
1255 ir_node *block = get_nodes_block(phi);
1256 ir_node *pred = get_Block_cfgpred(block, i);
1257 lattice_elem_t type = get_node_type(pred);
1259 return type.tv != tarval_unreachable;
1261 /* else it's the control input, always live */
1263 } /* is_live_input */
1266 * Return non-zero if a type is a constant.
1268 static int is_constant_type(lattice_elem_t type) {
1269 if (type.tv != tarval_bottom && type.tv != tarval_top)
1272 } /* is_constant_type */
1275 * Check whether a type is neither Top or a constant.
1276 * Note: U is handled like Top here, R is a constant.
1278 * @param type the type to check
1280 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1281 if (is_tarval(type.tv)) {
1282 if (type.tv == tarval_top)
1284 if (tarval_is_constant(type.tv))
1291 } /* type_is_neither_top_nor_const */
1294 * Collect nodes to the touched list.
1296 * @param list the list which contains the nodes that must be evaluated
1297 * @param idx the index of the def_use edge to evaluate
1298 * @param env the environment
1300 static void collect_touched(list_head *list, int idx, environment_t *env) {
1302 int end_idx = env->end_idx;
1304 list_for_each_entry(node_t, x, list, node_list) {
1308 /* leader edges start AFTER follower edges */
1309 x->next_edge = x->n_followers + 1;
1311 num_edges = get_irn_n_outs(x->node);
1313 /* for all edges in x.L.def_use_{idx} */
1314 while (x->next_edge <= num_edges) {
1315 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1318 /* check if we have necessary edges */
1319 if (edge->pos > idx)
1326 /* only non-commutative nodes */
1327 if (env->commutative &&
1328 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1331 /* ignore the "control input" for non-pinned nodes
1332 if we are running in GCSE mode */
1333 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1336 y = get_irn_node(succ);
1337 assert(get_irn_n(succ, idx) == x->node);
1339 /* ignore block edges touching followers */
1340 if (idx == -1 && y->is_follower)
1343 if (is_constant_type(y->type)) {
1344 ir_opcode code = get_irn_opcode(succ);
1345 if (code == iro_Sub || code == iro_Cmp)
1346 add_to_cprop(y, env);
1349 /* Partitions of constants should not be split simply because their Nodes have unequal
1350 functions or incongruent inputs. */
1351 if (type_is_neither_top_nor_const(y->type) &&
1352 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1353 add_to_touched(y, env);
1357 } /* collect_touched */
1360 * Collect commutative nodes to the touched list.
1362 * @param list the list which contains the nodes that must be evaluated
1363 * @param env the environment
1365 static void collect_commutative_touched(list_head *list, environment_t *env) {
1368 list_for_each_entry(node_t, x, list, node_list) {
1371 num_edges = get_irn_n_outs(x->node);
1373 x->next_edge = x->n_followers + 1;
1375 /* for all edges in x.L.def_use_{idx} */
1376 while (x->next_edge <= num_edges) {
1377 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1380 /* check if we have necessary edges */
1390 /* only commutative nodes */
1391 if (!is_op_commutative(get_irn_op(succ)))
1394 y = get_irn_node(succ);
1395 if (is_constant_type(y->type)) {
1396 ir_opcode code = get_irn_opcode(succ);
1397 if (code == iro_Eor)
1398 add_to_cprop(y, env);
1401 /* Partitions of constants should not be split simply because their Nodes have unequal
1402 functions or incongruent inputs. */
1403 if (type_is_neither_top_nor_const(y->type)) {
1404 add_to_touched(y, env);
1408 } /* collect_commutative_touched */
1411 * Split the partitions if caused by the first entry on the worklist.
1413 * @param env the environment
1415 static void cause_splits(environment_t *env) {
1416 partition_t *X, *Z, *N;
1419 /* remove the first partition from the worklist */
1421 env->worklist = X->wl_next;
1424 dump_partition("Cause_split: ", X);
1426 if (env->commutative) {
1427 /* handle commutative nodes first */
1429 /* empty the touched set: already done, just clear the list */
1430 env->touched = NULL;
1432 collect_commutative_touched(&X->Leader, env);
1433 collect_commutative_touched(&X->Follower, env);
1435 for (Z = env->touched; Z != NULL; Z = N) {
1437 node_t *touched = Z->touched;
1438 unsigned n_touched = Z->n_touched;
1440 assert(Z->touched != NULL);
1442 /* beware, split might change Z */
1443 N = Z->touched_next;
1445 /* remove it from the touched set */
1448 /* Empty local Z.touched. */
1449 for (e = touched; e != NULL; e = e->next) {
1450 assert(e->is_follower == 0);
1456 if (0 < n_touched && n_touched < Z->n_leader) {
1457 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1458 split(&Z, touched, env);
1460 assert(n_touched <= Z->n_leader);
1464 /* combine temporary leader and follower list */
1465 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1466 /* empty the touched set: already done, just clear the list */
1467 env->touched = NULL;
1469 collect_touched(&X->Leader, idx, env);
1470 collect_touched(&X->Follower, idx, env);
1472 for (Z = env->touched; Z != NULL; Z = N) {
1474 node_t *touched = Z->touched;
1475 unsigned n_touched = Z->n_touched;
1477 assert(Z->touched != NULL);
1479 /* beware, split might change Z */
1480 N = Z->touched_next;
1482 /* remove it from the touched set */
1485 /* Empty local Z.touched. */
1486 for (e = touched; e != NULL; e = e->next) {
1487 assert(e->is_follower == 0);
1493 if (0 < n_touched && n_touched < Z->n_leader) {
1494 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1495 split(&Z, touched, env);
1497 assert(n_touched <= Z->n_leader);
1500 } /* cause_splits */
1503 * Implements split_by_what(): Split a partition by characteristics given
1504 * by the what function.
1506 * @param X the partition to split
1507 * @param What a function returning an Id for every node of the partition X
1508 * @param P a list to store the result partitions
1509 * @param env the environment
1513 static partition_t *split_by_what(partition_t *X, what_func What,
1514 partition_t **P, environment_t *env) {
1517 listmap_entry_t *iter;
1520 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1522 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1523 void *id = What(x, env);
1524 listmap_entry_t *entry;
1527 /* input not allowed, ignore */
1530 /* Add x to map[What(x)]. */
1531 entry = listmap_find(&map, id);
1532 x->next = entry->list;
1535 /* Let P be a set of Partitions. */
1537 /* for all sets S except one in the range of map do */
1538 for (iter = map.values; iter != NULL; iter = iter->next) {
1539 if (iter->next == NULL) {
1540 /* this is the last entry, ignore */
1545 /* Add SPLIT( X, S ) to P. */
1546 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1547 R = split(&X, S, env);
1557 } /* split_by_what */
1559 /** lambda n.(n.type) */
1560 static void *lambda_type(const node_t *node, environment_t *env) {
1562 return node->type.tv;
1565 /** lambda n.(n.opcode) */
1566 static void *lambda_opcode(const node_t *node, environment_t *env) {
1567 opcode_key_t key, *entry;
1568 ir_node *irn = node->node;
1570 key.code = get_irn_opcode(irn);
1571 key.mode = get_irn_mode(irn);
1572 key.arity = get_irn_arity(irn);
1576 switch (get_irn_opcode(irn)) {
1578 key.u.proj = get_Proj_proj(irn);
1581 key.u.ent = get_Sel_entity(irn);
1587 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1589 } /* lambda_opcode */
1591 /** lambda n.(n[i].partition) */
1592 static void *lambda_partition(const node_t *node, environment_t *env) {
1593 ir_node *skipped = skip_Proj(node->node);
1596 int i = env->lambda_input;
1598 if (i >= get_irn_arity(node->node)) {
1600 * We are outside the allowed range: This can happen even
1601 * if we have split by opcode first: doing so might move Followers
1602 * to Leaders and those will have a different opcode!
1603 * Note that in this case the partition is on the cprop list and will be
1609 /* ignore the "control input" for non-pinned nodes
1610 if we are running in GCSE mode */
1611 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1614 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1615 p = get_irn_node(pred);
1618 } /* lambda_partition */
1620 /** lambda n.(n[i].partition) for commutative nodes */
1621 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1622 ir_node *irn = node->node;
1623 ir_node *skipped = skip_Proj(irn);
1624 ir_node *pred, *left, *right;
1626 partition_t *pl, *pr;
1627 int i = env->lambda_input;
1629 if (i >= get_irn_arity(node->node)) {
1631 * We are outside the allowed range: This can happen even
1632 * if we have split by opcode first: doing so might move Followers
1633 * to Leaders and those will have a different opcode!
1634 * Note that in this case the partition is on the cprop list and will be
1640 /* ignore the "control input" for non-pinned nodes
1641 if we are running in GCSE mode */
1642 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1646 pred = get_irn_n(skipped, i);
1647 p = get_irn_node(pred);
1651 if (is_op_commutative(get_irn_op(irn))) {
1652 /* normalize partition order by returning the "smaller" on input 0,
1653 the "bigger" on input 1. */
1654 left = get_binop_left(irn);
1655 pl = get_irn_node(left)->part;
1656 right = get_binop_right(irn);
1657 pr = get_irn_node(right)->part;
1660 return pl < pr ? pl : pr;
1662 return pl > pr ? pl : pr;
1664 /* a not split out Follower */
1665 pred = get_irn_n(irn, i);
1666 p = get_irn_node(pred);
1670 } /* lambda_commutative_partition */
1673 * Returns true if a type is a constant.
1675 static int is_con(const lattice_elem_t type) {
1676 /* be conservative */
1677 if (is_tarval(type.tv))
1678 return tarval_is_constant(type.tv);
1679 return is_entity(type.sym.entity_p);
1683 * Implements split_by().
1685 * @param X the partition to split
1686 * @param env the environment
1688 static void split_by(partition_t *X, environment_t *env) {
1689 partition_t *I, *P = NULL;
1692 dump_partition("split_by", X);
1694 if (X->n_leader == 1) {
1695 /* we have only one leader, no need to split, just check it's type */
1696 node_t *x = get_first_node(X);
1697 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1701 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1702 P = split_by_what(X, lambda_type, &P, env);
1705 /* adjust the type tags, we have split partitions by type */
1706 for (I = P; I != NULL; I = I->split_next) {
1707 node_t *x = get_first_node(I);
1708 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1715 if (Y->n_leader > 1) {
1716 /* we do not want split the TOP or constant partitions */
1717 if (! Y->type_is_T_or_C) {
1718 partition_t *Q = NULL;
1720 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1721 Q = split_by_what(Y, lambda_opcode, &Q, env);
1728 if (Z->n_leader > 1) {
1729 const node_t *first = get_first_node(Z);
1730 int arity = get_irn_arity(first->node);
1732 what_func what = lambda_partition;
1733 DEBUG_ONLY(char buf[64];)
1735 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1736 what = lambda_commutative_partition;
1739 * BEWARE: during splitting by input 2 for instance we might
1740 * create new partitions which are different by input 1, so collect
1741 * them and split further.
1743 Z->split_next = NULL;
1746 for (input = arity - 1; input >= -1; --input) {
1748 partition_t *Z_prime = R;
1751 if (Z_prime->n_leader > 1) {
1752 env->lambda_input = input;
1753 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1754 DEBUG_ONLY(what_reason = buf;)
1755 S = split_by_what(Z_prime, what, &S, env);
1758 Z_prime->split_next = S;
1761 } while (R != NULL);
1766 } while (Q != NULL);
1769 } while (P != NULL);
1773 * (Re-)compute the type for a given node.
1775 * @param node the node
1777 static void default_compute(node_t *node) {
1779 ir_node *irn = node->node;
1780 node_t *block = get_irn_node(get_nodes_block(irn));
1782 if (block->type.tv == tarval_unreachable) {
1783 node->type.tv = tarval_top;
1787 /* if any of the data inputs have type top, the result is type top */
1788 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1789 ir_node *pred = get_irn_n(irn, i);
1790 node_t *p = get_irn_node(pred);
1792 if (p->type.tv == tarval_top) {
1793 node->type.tv = tarval_top;
1798 if (get_irn_mode(node->node) == mode_X)
1799 node->type.tv = tarval_reachable;
1801 node->type.tv = computed_value(irn);
1802 } /* default_compute */
1805 * (Re-)compute the type for a Block node.
1807 * @param node the node
1809 static void compute_Block(node_t *node) {
1811 ir_node *block = node->node;
1813 if (block == get_irg_start_block(current_ir_graph)) {
1814 /* start block is always reachable */
1815 node->type.tv = tarval_reachable;
1819 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1820 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1822 if (pred->type.tv == tarval_reachable) {
1823 /* A block is reachable, if at least of predecessor is reachable. */
1824 node->type.tv = tarval_reachable;
1828 node->type.tv = tarval_top;
1829 } /* compute_Block */
1832 * (Re-)compute the type for a Bad node.
1834 * @param node the node
1836 static void compute_Bad(node_t *node) {
1837 /* Bad nodes ALWAYS compute Top */
1838 node->type.tv = tarval_top;
1842 * (Re-)compute the type for an Unknown node.
1844 * @param node the node
1846 static void compute_Unknown(node_t *node) {
1847 /* While Unknown nodes should compute Top this is dangerous:
1848 * a Top input to a Cond would lead to BOTH control flows unreachable.
1849 * While this is correct in the given semantics, it would destroy the Firm
1852 * It would be safe to compute Top IF it can be assured, that only Cmp
1853 * nodes are inputs to Conds. We check that first.
1854 * This is the way Frontends typically build Firm, but some optimizations
1855 * (cond_eval for instance) might replace them by Phib's...
1857 node->type.tv = tarval_UNKNOWN;
1858 } /* compute_Unknown */
1861 * (Re-)compute the type for a Jmp node.
1863 * @param node the node
1865 static void compute_Jmp(node_t *node) {
1866 node_t *block = get_irn_node(get_nodes_block(node->node));
1868 node->type = block->type;
1872 * (Re-)compute the type for the End node.
1874 * @param node the node
1876 static void compute_End(node_t *node) {
1877 /* the End node is NOT dead of course */
1878 node->type.tv = tarval_reachable;
1882 * (Re-)compute the type for a SymConst node.
1884 * @param node the node
1886 static void compute_SymConst(node_t *node) {
1887 ir_node *irn = node->node;
1888 node_t *block = get_irn_node(get_nodes_block(irn));
1890 if (block->type.tv == tarval_unreachable) {
1891 node->type.tv = tarval_top;
1894 switch (get_SymConst_kind(irn)) {
1895 case symconst_addr_ent:
1896 /* case symconst_addr_name: cannot handle this yet */
1897 node->type.sym = get_SymConst_symbol(irn);
1900 node->type.tv = computed_value(irn);
1902 } /* compute_SymConst */
1905 * (Re-)compute the type for a Phi node.
1907 * @param node the node
1909 static void compute_Phi(node_t *node) {
1911 ir_node *phi = node->node;
1912 lattice_elem_t type;
1914 /* if a Phi is in a unreachable block, its type is TOP */
1915 node_t *block = get_irn_node(get_nodes_block(phi));
1917 if (block->type.tv == tarval_unreachable) {
1918 node->type.tv = tarval_top;
1922 /* Phi implements the Meet operation */
1923 type.tv = tarval_top;
1924 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1925 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1926 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1928 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1929 /* ignore TOP inputs: We must check here for unreachable blocks,
1930 because Firm constants live in the Start Block are NEVER Top.
1931 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1932 comes from a unreachable input. */
1935 if (pred->type.tv == tarval_bottom) {
1936 node->type.tv = tarval_bottom;
1938 } else if (type.tv == tarval_top) {
1939 /* first constant found */
1941 } else if (type.tv != pred->type.tv) {
1942 /* different constants or tarval_bottom */
1943 node->type.tv = tarval_bottom;
1946 /* else nothing, constants are the same */
1952 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1954 * @param node the node
1956 static void compute_Add(node_t *node) {
1957 ir_node *sub = node->node;
1958 node_t *l = get_irn_node(get_Add_left(sub));
1959 node_t *r = get_irn_node(get_Add_right(sub));
1960 lattice_elem_t a = l->type;
1961 lattice_elem_t b = r->type;
1964 if (a.tv == tarval_top || b.tv == tarval_top) {
1965 node->type.tv = tarval_top;
1966 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1967 node->type.tv = tarval_bottom;
1969 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1970 must call tarval_add() first to handle this case! */
1971 if (is_tarval(a.tv)) {
1972 if (is_tarval(b.tv)) {
1973 node->type.tv = tarval_add(a.tv, b.tv);
1976 mode = get_tarval_mode(a.tv);
1977 if (a.tv == get_mode_null(mode)) {
1981 } else if (is_tarval(b.tv)) {
1982 mode = get_tarval_mode(b.tv);
1983 if (b.tv == get_mode_null(mode)) {
1988 node->type.tv = tarval_bottom;
1993 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1995 * @param node the node
1997 static void compute_Sub(node_t *node) {
1998 ir_node *sub = node->node;
1999 node_t *l = get_irn_node(get_Sub_left(sub));
2000 node_t *r = get_irn_node(get_Sub_right(sub));
2001 lattice_elem_t a = l->type;
2002 lattice_elem_t b = r->type;
2005 if (a.tv == tarval_top || b.tv == tarval_top) {
2006 node->type.tv = tarval_top;
2007 } else if (is_con(a) && is_con(b)) {
2008 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2009 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2010 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2012 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2015 node->type.tv = tarval_bottom;
2017 node->by_all_const = 1;
2018 } else if (r->part == l->part &&
2019 (!mode_is_float(get_irn_mode(l->node)))) {
2021 * BEWARE: a - a is NOT always 0 for floating Point values, as
2022 * NaN op NaN = NaN, so we must check this here.
2024 ir_mode *mode = get_irn_mode(sub);
2025 tv = get_mode_null(mode);
2027 /* if the node was ONCE evaluated by all constants, but now
2028 this breaks AND we get from the argument partitions a different
2029 result, switch to bottom.
2030 This happens because initially all nodes are in the same partition ... */
2031 if (node->by_all_const && node->type.tv != tv)
2035 node->type.tv = tarval_bottom;
2040 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2042 * @param node the node
2044 static void compute_Eor(node_t *node) {
2045 ir_node *eor = node->node;
2046 node_t *l = get_irn_node(get_Eor_left(eor));
2047 node_t *r = get_irn_node(get_Eor_right(eor));
2048 lattice_elem_t a = l->type;
2049 lattice_elem_t b = r->type;
2052 if (a.tv == tarval_top || b.tv == tarval_top) {
2053 node->type.tv = tarval_top;
2054 } else if (is_con(a) && is_con(b)) {
2055 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2056 node->type.tv = tarval_eor(a.tv, b.tv);
2057 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2059 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2062 node->type.tv = tarval_bottom;
2064 node->by_all_const = 1;
2065 } else if (r->part == l->part) {
2066 ir_mode *mode = get_irn_mode(eor);
2067 tv = get_mode_null(mode);
2069 /* if the node was ONCE evaluated by all constants, but now
2070 this breaks AND we get from the argument partitions a different
2071 result, switch to bottom.
2072 This happens because initially all nodes are in the same partition ... */
2073 if (node->by_all_const && node->type.tv != tv)
2077 node->type.tv = tarval_bottom;
2082 * (Re-)compute the type for Cmp.
2084 * @param node the node
2086 static void compute_Cmp(node_t *node) {
2087 ir_node *cmp = node->node;
2088 node_t *l = get_irn_node(get_Cmp_left(cmp));
2089 node_t *r = get_irn_node(get_Cmp_right(cmp));
2090 lattice_elem_t a = l->type;
2091 lattice_elem_t b = r->type;
2093 if (a.tv == tarval_top || b.tv == tarval_top) {
2096 * Top is congruent to any other value, we can
2097 * calculate the compare result.
2099 node->type.tv = tarval_b_true;
2101 node->type.tv = tarval_top;
2103 } else if (is_con(a) && is_con(b)) {
2104 /* both nodes are constants, we can probably do something */
2105 node->type.tv = tarval_b_true;
2106 } else if (r->part == l->part) {
2107 /* both nodes congruent, we can probably do something */
2108 node->type.tv = tarval_b_true;
2110 node->type.tv = tarval_bottom;
2112 } /* compute_Proj_Cmp */
2115 * (Re-)compute the type for a Proj(Cmp).
2117 * @param node the node
2118 * @param cond the predecessor Cmp node
2120 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2121 ir_node *proj = node->node;
2122 node_t *l = get_irn_node(get_Cmp_left(cmp));
2123 node_t *r = get_irn_node(get_Cmp_right(cmp));
2124 lattice_elem_t a = l->type;
2125 lattice_elem_t b = r->type;
2126 pn_Cmp pnc = get_Proj_proj(proj);
2129 if (a.tv == tarval_top || b.tv == tarval_top) {
2132 tv = new_tarval_from_long((pnc & pn_Cmp_Eq) ^ pn_Cmp_Eq, mode_b);
2135 node->type.tv = tarval_top;
2137 } else if (is_con(a) && is_con(b)) {
2138 default_compute(node);
2139 node->by_all_const = 1;
2140 } else if (r->part == l->part &&
2141 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2143 * BEWARE: a == a is NOT always True for floating Point values, as
2144 * NaN != NaN is defined, so we must check this here.
2146 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
2151 /* if the node was ONCE evaluated by all constants, but now
2152 this breaks AND we get from the argument partitions a different
2153 result, switch to bottom.
2154 This happens because initially all nodes are in the same partition ... */
2155 if (node->by_all_const && node->type.tv != tv)
2159 node->type.tv = tarval_bottom;
2161 } /* compute_Proj_Cmp */
2164 * (Re-)compute the type for a Proj(Cond).
2166 * @param node the node
2167 * @param cond the predecessor Cond node
2169 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2170 ir_node *proj = node->node;
2171 long pnc = get_Proj_proj(proj);
2172 ir_node *sel = get_Cond_selector(cond);
2173 node_t *selector = get_irn_node(sel);
2175 if (get_irn_mode(sel) == mode_b) {
2177 if (pnc == pn_Cond_true) {
2178 if (selector->type.tv == tarval_b_false) {
2179 node->type.tv = tarval_unreachable;
2180 } else if (selector->type.tv == tarval_b_true) {
2181 node->type.tv = tarval_reachable;
2182 } else if (selector->type.tv == tarval_bottom) {
2183 node->type.tv = tarval_reachable;
2185 assert(selector->type.tv == tarval_top);
2186 node->type.tv = tarval_unreachable;
2189 assert(pnc == pn_Cond_false);
2191 if (selector->type.tv == tarval_b_false) {
2192 node->type.tv = tarval_reachable;
2193 } else if (selector->type.tv == tarval_b_true) {
2194 node->type.tv = tarval_unreachable;
2195 } else if (selector->type.tv == tarval_bottom) {
2196 node->type.tv = tarval_reachable;
2198 assert(selector->type.tv == tarval_top);
2199 node->type.tv = tarval_unreachable;
2204 if (selector->type.tv == tarval_bottom) {
2205 node->type.tv = tarval_reachable;
2206 } else if (selector->type.tv == tarval_top) {
2207 node->type.tv = tarval_unreachable;
2209 long value = get_tarval_long(selector->type.tv);
2210 if (pnc == get_Cond_defaultProj(cond)) {
2211 /* default switch, have to check ALL other cases */
2214 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2215 ir_node *succ = get_irn_out(cond, i);
2219 if (value == get_Proj_proj(succ)) {
2220 /* we found a match, will NOT take the default case */
2221 node->type.tv = tarval_unreachable;
2225 /* all cases checked, no match, will take default case */
2226 node->type.tv = tarval_reachable;
2229 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2233 } /* compute_Proj_Cond */
2236 * (Re-)compute the type for a Proj-Node.
2238 * @param node the node
2240 static void compute_Proj(node_t *node) {
2241 ir_node *proj = node->node;
2242 ir_mode *mode = get_irn_mode(proj);
2243 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2244 ir_node *pred = get_Proj_pred(proj);
2246 if (block->type.tv == tarval_unreachable) {
2247 /* a Proj in a unreachable Block stay Top */
2248 node->type.tv = tarval_top;
2251 if (get_irn_node(pred)->type.tv == tarval_top) {
2252 /* if the predecessor is Top, its Proj follow */
2253 node->type.tv = tarval_top;
2257 if (mode == mode_M) {
2258 /* mode M is always bottom */
2259 node->type.tv = tarval_bottom;
2262 if (mode != mode_X) {
2264 compute_Proj_Cmp(node, pred);
2266 default_compute(node);
2269 /* handle mode_X nodes */
2271 switch (get_irn_opcode(pred)) {
2273 /* the Proj_X from the Start is always reachable.
2274 However this is already handled at the top. */
2275 node->type.tv = tarval_reachable;
2278 compute_Proj_Cond(node, pred);
2281 default_compute(node);
2283 } /* compute_Proj */
2286 * (Re-)compute the type for a Confirm.
2288 * @param node the node
2290 static void compute_Confirm(node_t *node) {
2291 ir_node *confirm = node->node;
2292 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2294 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2295 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2297 if (is_con(bound->type)) {
2298 /* is equal to a constant */
2299 node->type = bound->type;
2303 /* a Confirm is a copy OR a Const */
2304 node->type = pred->type;
2305 } /* compute_Confirm */
2308 * (Re-)compute the type for a Max.
2310 * @param node the node
2312 static void compute_Max(node_t *node) {
2313 ir_node *op = node->node;
2314 node_t *l = get_irn_node(get_binop_left(op));
2315 node_t *r = get_irn_node(get_binop_right(op));
2316 lattice_elem_t a = l->type;
2317 lattice_elem_t b = r->type;
2319 if (a.tv == tarval_top || b.tv == tarval_top) {
2320 node->type.tv = tarval_top;
2321 } else if (is_con(a) && is_con(b)) {
2322 /* both nodes are constants, we can probably do something */
2324 /* this case handles SymConsts as well */
2327 ir_mode *mode = get_irn_mode(op);
2328 tarval *tv_min = get_mode_min(mode);
2332 else if (b.tv == tv_min)
2334 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2335 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2336 node->type.tv = a.tv;
2338 node->type.tv = b.tv;
2340 node->type.tv = tarval_bad;
2343 } else if (r->part == l->part) {
2344 /* both nodes congruent, we can probably do something */
2347 node->type.tv = tarval_bottom;
2352 * (Re-)compute the type for a Min.
2354 * @param node the node
2356 static void compute_Min(node_t *node) {
2357 ir_node *op = node->node;
2358 node_t *l = get_irn_node(get_binop_left(op));
2359 node_t *r = get_irn_node(get_binop_right(op));
2360 lattice_elem_t a = l->type;
2361 lattice_elem_t b = r->type;
2363 if (a.tv == tarval_top || b.tv == tarval_top) {
2364 node->type.tv = tarval_top;
2365 } else if (is_con(a) && is_con(b)) {
2366 /* both nodes are constants, we can probably do something */
2368 /* this case handles SymConsts as well */
2371 ir_mode *mode = get_irn_mode(op);
2372 tarval *tv_max = get_mode_max(mode);
2376 else if (b.tv == tv_max)
2378 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2379 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2380 node->type.tv = a.tv;
2382 node->type.tv = b.tv;
2384 node->type.tv = tarval_bad;
2387 } else if (r->part == l->part) {
2388 /* both nodes congruent, we can probably do something */
2391 node->type.tv = tarval_bottom;
2396 * (Re-)compute the type for a given node.
2398 * @param node the node
2400 static void compute(node_t *node) {
2403 if (is_no_Block(node->node)) {
2404 node_t *block = get_irn_node(get_nodes_block(node->node));
2406 if (block->type.tv == tarval_unreachable) {
2407 node->type.tv = tarval_top;
2412 func = (compute_func)node->node->op->ops.generic;
2418 * Identity functions: Note that one might thing that identity() is just a
2419 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2420 * here, because it expects that the identity node is one of the inputs, which is NOT
2421 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2422 * So, we have our own implementation, which copies some parts of equivalent_node()
2426 * Calculates the Identity for Phi nodes
2428 static node_t *identity_Phi(node_t *node) {
2429 ir_node *phi = node->node;
2430 ir_node *block = get_nodes_block(phi);
2431 node_t *n_part = NULL;
2434 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2435 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2437 if (pred_X->type.tv == tarval_reachable) {
2438 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2442 else if (n_part->part != pred->part) {
2443 /* incongruent inputs, not a follower */
2448 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2449 * tarval_top, is in the TOP partition and should NOT being split! */
2450 assert(n_part != NULL);
2452 } /* identity_Phi */
2455 * Calculates the Identity for commutative 0 neutral nodes.
2457 static node_t *identity_comm_zero_binop(node_t *node) {
2458 ir_node *op = node->node;
2459 node_t *a = get_irn_node(get_binop_left(op));
2460 node_t *b = get_irn_node(get_binop_right(op));
2461 ir_mode *mode = get_irn_mode(op);
2464 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2465 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2468 /* node: no input should be tarval_top, else the binop would be also
2469 * Top and not being split. */
2470 zero = get_mode_null(mode);
2471 if (a->type.tv == zero)
2473 if (b->type.tv == zero)
2476 } /* identity_comm_zero_binop */
2479 * Calculates the Identity for Shift nodes.
2481 static node_t *identity_shift(node_t *node) {
2482 ir_node *op = node->node;
2483 node_t *b = get_irn_node(get_binop_right(op));
2484 ir_mode *mode = get_irn_mode(b->node);
2487 /* node: no input should be tarval_top, else the binop would be also
2488 * Top and not being split. */
2489 zero = get_mode_null(mode);
2490 if (b->type.tv == zero)
2491 return get_irn_node(get_binop_left(op));
2493 } /* identity_shift */
2496 * Calculates the Identity for Mul nodes.
2498 static node_t *identity_Mul(node_t *node) {
2499 ir_node *op = node->node;
2500 node_t *a = get_irn_node(get_Mul_left(op));
2501 node_t *b = get_irn_node(get_Mul_right(op));
2502 ir_mode *mode = get_irn_mode(op);
2505 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2506 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2509 /* node: no input should be tarval_top, else the binop would be also
2510 * Top and not being split. */
2511 one = get_mode_one(mode);
2512 if (a->type.tv == one)
2514 if (b->type.tv == one)
2517 } /* identity_Mul */
2520 * Calculates the Identity for Sub nodes.
2522 static node_t *identity_Sub(node_t *node) {
2523 ir_node *sub = node->node;
2524 node_t *b = get_irn_node(get_Sub_right(sub));
2525 ir_mode *mode = get_irn_mode(sub);
2527 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2528 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2531 /* node: no input should be tarval_top, else the binop would be also
2532 * Top and not being split. */
2533 if (b->type.tv == get_mode_null(mode))
2534 return get_irn_node(get_Sub_left(sub));
2536 } /* identity_Sub */
2539 * Calculates the Identity for And nodes.
2541 static node_t *identity_And(node_t *node) {
2542 ir_node *and = node->node;
2543 node_t *a = get_irn_node(get_And_left(and));
2544 node_t *b = get_irn_node(get_And_right(and));
2545 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2547 /* node: no input should be tarval_top, else the And would be also
2548 * Top and not being split. */
2549 if (a->type.tv == neutral)
2551 if (b->type.tv == neutral)
2554 } /* identity_And */
2557 * Calculates the Identity for Confirm nodes.
2559 static node_t *identity_Confirm(node_t *node) {
2560 ir_node *confirm = node->node;
2562 /* a Confirm is always a Copy */
2563 return get_irn_node(get_Confirm_value(confirm));
2564 } /* identity_Confirm */
2567 * Calculates the Identity for Mux nodes.
2569 static node_t *identity_Mux(node_t *node) {
2570 ir_node *mux = node->node;
2571 node_t *t = get_irn_node(get_Mux_true(mux));
2572 node_t *f = get_irn_node(get_Mux_false(mux));
2575 if (t->part == f->part)
2578 /* for now, the 1-input identity is not supported */
2580 sel = get_irn_node(get_Mux_sel(mux));
2582 /* Mux sel input is mode_b, so it is always a tarval */
2583 if (sel->type.tv == tarval_b_true)
2585 if (sel->type.tv == tarval_b_false)
2589 } /* identity_Mux */
2592 * Calculates the Identity for Min nodes.
2594 static node_t *identity_Min(node_t *node) {
2595 ir_node *op = node->node;
2596 node_t *a = get_irn_node(get_binop_left(op));
2597 node_t *b = get_irn_node(get_binop_right(op));
2598 ir_mode *mode = get_irn_mode(op);
2601 if (a->part == b->part) {
2602 /* leader of multiple predecessors */
2606 /* works even with NaN */
2607 tv_max = get_mode_max(mode);
2608 if (a->type.tv == tv_max)
2610 if (b->type.tv == tv_max)
2613 } /* identity_Min */
2616 * Calculates the Identity for Max nodes.
2618 static node_t *identity_Max(node_t *node) {
2619 ir_node *op = node->node;
2620 node_t *a = get_irn_node(get_binop_left(op));
2621 node_t *b = get_irn_node(get_binop_right(op));
2622 ir_mode *mode = get_irn_mode(op);
2625 if (a->part == b->part) {
2626 /* leader of multiple predecessors */
2630 /* works even with NaN */
2631 tv_min = get_mode_min(mode);
2632 if (a->type.tv == tv_min)
2634 if (b->type.tv == tv_min)
2637 } /* identity_Max */
2640 * Calculates the Identity for nodes.
2642 static node_t *identity(node_t *node) {
2643 ir_node *irn = node->node;
2645 switch (get_irn_opcode(irn)) {
2647 return identity_Phi(node);
2649 return identity_Mul(node);
2653 return identity_comm_zero_binop(node);
2658 return identity_shift(node);
2660 return identity_And(node);
2662 return identity_Sub(node);
2664 return identity_Confirm(node);
2666 return identity_Mux(node);
2668 return identity_Min(node);
2670 return identity_Max(node);
2677 * Node follower is a (new) follower of leader, segregate Leader
2680 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2681 ir_node *l = leader->node;
2682 int j, i, n = get_irn_n_outs(l);
2684 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2685 /* The leader edges must remain sorted, but follower edges can
2687 for (i = leader->n_followers + 1; i <= n; ++i) {
2688 if (l->out[i].use == follower) {
2689 ir_def_use_edge t = l->out[i];
2691 for (j = i - 1; j >= leader->n_followers + 1; --j)
2692 l->out[j + 1] = l->out[j];
2693 ++leader->n_followers;
2694 l->out[leader->n_followers] = t;
2698 } /* segregate_def_use_chain_1 */
2701 * Node follower is a (new) follower segregate its Leader
2704 * @param follower the follower IR node
2706 static void segregate_def_use_chain(const ir_node *follower) {
2709 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2710 node_t *pred = get_irn_node(get_irn_n(follower, i));
2712 segregate_def_use_chain_1(follower, pred);
2714 } /* segregate_def_use_chain */
2717 * Propagate constant evaluation.
2719 * @param env the environment
2721 static void propagate(environment_t *env) {
2724 lattice_elem_t old_type;
2726 unsigned n_fallen, old_type_was_T_or_C;
2729 while (env->cprop != NULL) {
2730 void *oldopcode = NULL;
2732 /* remove the first partition X from cprop */
2735 env->cprop = X->cprop_next;
2737 old_type_was_T_or_C = X->type_is_T_or_C;
2739 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2742 while (! list_empty(&X->cprop)) {
2743 /* remove the first Node x from X.cprop */
2744 x = list_entry(X->cprop.next, node_t, cprop_list);
2745 //assert(x->part == X);
2746 list_del(&x->cprop_list);
2749 if (x->is_follower && identity(x) == x) {
2750 /* check the opcode first */
2751 if (oldopcode == NULL) {
2752 oldopcode = lambda_opcode(get_first_node(X), env);
2754 if (oldopcode != lambda_opcode(x, env)) {
2755 if (x->on_fallen == 0) {
2756 /* different opcode -> x falls out of this partition */
2761 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2765 /* x will make the follower -> leader transition */
2766 follower_to_leader(x);
2769 /* compute a new type for x */
2771 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2773 if (x->type.tv != old_type.tv) {
2774 verify_type(old_type, x->type);
2775 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2777 if (x->on_fallen == 0) {
2778 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2779 not already on the list. */
2784 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2786 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2787 ir_node *succ = get_irn_out(x->node, i);
2788 node_t *y = get_irn_node(succ);
2790 /* Add y to y.partition.cprop. */
2791 add_to_cprop(y, env);
2796 if (n_fallen > 0 && n_fallen != X->n_leader) {
2797 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2798 Y = split(&X, fallen, env);
2800 * We have split out fallen node. The type of the result
2801 * partition is NOT set yet.
2803 Y->type_is_T_or_C = 0;
2807 /* remove the flags from the fallen list */
2808 for (x = fallen; x != NULL; x = x->next)
2811 if (old_type_was_T_or_C) {
2814 /* check if some nodes will make the leader -> follower transition */
2815 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2816 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2817 node_t *eq_node = identity(y);
2819 if (eq_node != y && eq_node->part == y->part) {
2820 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2821 /* move to Follower */
2823 list_del(&y->node_list);
2824 list_add_tail(&y->node_list, &Y->Follower);
2827 segregate_def_use_chain(y->node);
2837 * Get the leader for a given node from its congruence class.
2839 * @param irn the node
2841 static ir_node *get_leader(node_t *node) {
2842 partition_t *part = node->part;
2844 if (part->n_leader > 1 || node->is_follower) {
2845 if (node->is_follower) {
2846 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2849 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2851 return get_first_node(part)->node;
2857 * Return non-zero if the control flow predecessor node pred
2858 * is the only reachable control flow exit of its block.
2860 * @param pred the control flow exit
2862 static int can_exchange(ir_node *pred) {
2865 else if (is_Jmp(pred))
2867 else if (get_irn_mode(pred) == mode_T) {
2870 /* if the predecessor block has more than one
2871 reachable outputs we cannot remove the block */
2873 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2874 ir_node *proj = get_irn_out(pred, i);
2877 /* skip non-control flow Proj's */
2878 if (get_irn_mode(proj) != mode_X)
2881 node = get_irn_node(proj);
2882 if (node->type.tv == tarval_reachable) {
2890 } /* can_exchange */
2893 * Block Post-Walker, apply the analysis results on control flow by
2894 * shortening Phi's and Block inputs.
2896 static void apply_cf(ir_node *block, void *ctx) {
2897 environment_t *env = ctx;
2898 node_t *node = get_irn_node(block);
2900 ir_node **ins, **in_X;
2901 ir_node *phi, *next;
2903 n = get_Block_n_cfgpreds(block);
2905 if (node->type.tv == tarval_unreachable) {
2908 for (i = n - 1; i >= 0; --i) {
2909 ir_node *pred = get_Block_cfgpred(block, i);
2911 if (! is_Bad(pred)) {
2912 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2914 if (pred_bl->flagged == 0) {
2915 pred_bl->flagged = 3;
2917 if (pred_bl->type.tv == tarval_reachable) {
2919 * We will remove an edge from block to its pred.
2920 * This might leave the pred block as an endless loop
2922 if (! is_backedge(block, i))
2923 keep_alive(pred_bl->node);
2929 /* the EndBlock is always reachable even if the analysis
2930 finds out the opposite :-) */
2931 if (block != get_irg_end_block(current_ir_graph)) {
2932 /* mark dead blocks */
2933 set_Block_dead(block);
2934 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
2936 /* the endblock is unreachable */
2937 set_irn_in(block, 0, NULL);
2943 /* only one predecessor combine */
2944 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2946 if (can_exchange(pred)) {
2947 ir_node *new_block = get_nodes_block(pred);
2948 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
2949 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2950 exchange(block, new_block);
2951 node->node = new_block;
2957 NEW_ARR_A(ir_node *, in_X, n);
2959 for (i = 0; i < n; ++i) {
2960 ir_node *pred = get_Block_cfgpred(block, i);
2961 node_t *node = get_irn_node(pred);
2963 if (node->type.tv == tarval_reachable) {
2966 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
2967 if (! is_Bad(pred)) {
2968 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2970 if (pred_bl->flagged == 0) {
2971 pred_bl->flagged = 3;
2973 if (pred_bl->type.tv == tarval_reachable) {
2975 * We will remove an edge from block to its pred.
2976 * This might leave the pred block as an endless loop
2978 if (! is_backedge(block, i))
2979 keep_alive(pred_bl->node);
2988 NEW_ARR_A(ir_node *, ins, n);
2989 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2990 node_t *node = get_irn_node(phi);
2992 next = get_Phi_next(phi);
2993 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2994 /* this Phi is replaced by a constant */
2995 tarval *tv = node->type.tv;
2996 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2998 set_irn_node(c, node);
3000 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3001 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3006 for (i = 0; i < n; ++i) {
3007 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3009 if (pred->type.tv == tarval_reachable) {
3010 ins[j++] = get_Phi_pred(phi, i);
3014 /* this Phi is replaced by a single predecessor */
3015 ir_node *s = ins[0];
3016 node_t *phi_node = get_irn_node(phi);
3019 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3020 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3025 set_irn_in(phi, j, ins);
3032 /* this Block has only one live predecessor */
3033 ir_node *pred = skip_Proj(in_X[0]);
3035 if (can_exchange(pred)) {
3036 ir_node *new_block = get_nodes_block(pred);
3037 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3038 exchange(block, new_block);
3039 node->node = new_block;
3043 set_irn_in(block, k, in_X);
3049 * Exchange a node by its leader.
3050 * Beware: in rare cases the mode might be wrong here, for instance
3051 * AddP(x, NULL) is a follower of x, but with different mode.
3054 static void exchange_leader(ir_node *irn, ir_node *leader) {
3055 ir_mode *mode = get_irn_mode(irn);
3056 if (mode != get_irn_mode(leader)) {
3057 /* The conv is a no-op, so we are fre to place in
3058 * either in the block of the leader OR in irn's block.
3059 * Propably placing it into leaders block might reduce
3060 * the number of Conv due to CSE. */
3061 ir_node *block = get_nodes_block(leader);
3062 dbg_info *dbg = get_irn_dbg_info(irn);
3064 leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
3066 exchange(irn, leader);
3070 * Post-Walker, apply the analysis results;
3072 static void apply_result(ir_node *irn, void *ctx) {
3073 environment_t *env = ctx;
3074 node_t *node = get_irn_node(irn);
3076 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3077 /* blocks already handled, do not touch the End node */
3079 node_t *block = get_irn_node(get_nodes_block(irn));
3081 if (block->type.tv == tarval_unreachable) {
3082 ir_node *bad = get_irg_bad(current_ir_graph);
3084 /* here, bad might already have a node, but this can be safely ignored
3085 as long as bad has at least ONE valid node */
3086 set_irn_node(bad, node);
3088 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3092 else if (node->type.tv == tarval_unreachable) {
3093 /* don't kick away Unknown */
3094 if (! is_Unknown(irn)) {
3095 ir_node *bad = get_irg_bad(current_ir_graph);
3097 /* see comment above */
3098 set_irn_node(bad, node);
3100 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3105 else if (get_irn_mode(irn) == mode_X) {
3108 ir_node *cond = get_Proj_pred(irn);
3110 if (is_Cond(cond)) {
3111 node_t *sel = get_irn_node(get_Cond_selector(cond));
3113 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
3114 /* Cond selector is a constant and the Proj is reachable, make a Jmp */
3115 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
3116 set_irn_node(jmp, node);
3118 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3119 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3126 /* normal data node */
3127 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3128 tarval *tv = node->type.tv;
3131 * Beware: never replace mode_T nodes by constants. Currently we must mark
3132 * mode_T nodes with constants, but do NOT replace them.
3134 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3135 /* can be replaced by a constant */
3136 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
3137 set_irn_node(c, node);
3139 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3140 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3141 exchange_leader(irn, c);
3144 } else if (is_entity(node->type.sym.entity_p)) {
3145 if (! is_SymConst(irn)) {
3146 /* can be replaced by a SymConst */
3147 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3148 set_irn_node(symc, node);
3151 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3152 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3153 exchange_leader(irn, symc);
3156 } else if (is_Confirm(irn)) {
3157 /* Confirms are always follower, but do not kill them here */
3159 ir_node *leader = get_leader(node);
3161 if (leader != irn) {
3162 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3163 if (node->is_follower)
3164 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3166 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3167 exchange_leader(irn, leader);
3173 } /* apply_result */
3176 * Fix the keep-alives by deleting unreachable ones.
3178 static void apply_end(ir_node *end, environment_t *env) {
3179 int i, j, n = get_End_n_keepalives(end);
3183 NEW_ARR_A(ir_node *, in, n);
3185 /* fix the keep alive */
3186 for (i = j = 0; i < n; i++) {
3187 ir_node *ka = get_End_keepalive(end, i);
3188 node_t *node = get_irn_node(ka);
3191 node = get_irn_node(get_nodes_block(ka));
3193 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3197 set_End_keepalives(end, j, in);
3202 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3205 * sets the generic functions to compute.
3207 static void set_compute_functions(void) {
3210 /* set the default compute function */
3211 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3212 ir_op *op = get_irp_opcode(i);
3213 op->ops.generic = (op_func)default_compute;
3216 /* set specific functions */
3236 } /* set_compute_functions */
3238 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
3239 #ifdef DEBUG_libfirm
3240 ir_node *irn = local != NULL ? local : n;
3241 node_t *node = get_irn_node(irn);
3243 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
3248 void combo(ir_graph *irg) {
3250 ir_node *initial_bl;
3252 ir_graph *rem = current_ir_graph;
3254 current_ir_graph = irg;
3256 /* register a debug mask */
3257 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3259 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3261 obstack_init(&env.obst);
3262 env.worklist = NULL;
3266 #ifdef DEBUG_libfirm
3267 env.dbg_list = NULL;
3269 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3270 env.type2id_map = pmap_create();
3271 env.end_idx = get_opt_global_cse() ? 0 : -1;
3272 env.lambda_input = 0;
3273 env.nonstd_cond = 0;
3274 env.commutative = 1;
3277 assure_irg_outs(irg);
3278 assure_cf_loop(irg);
3280 /* we have our own value_of function */
3281 set_value_of_func(get_node_tarval);
3283 set_compute_functions();
3284 DEBUG_ONLY(part_nr = 0);
3286 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
3288 /* create the initial partition and place it on the work list */
3289 env.initial = new_partition(&env);
3290 add_to_worklist(env.initial, &env);
3291 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
3294 tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
3296 tarval_UNKNOWN = tarval_bad;
3299 /* all nodes on the initial partition have type Top */
3300 env.initial->type_is_T_or_C = 1;
3302 /* Place the START Node's partition on cprop.
3303 Place the START Node on its local worklist. */
3304 initial_bl = get_irg_start_block(irg);
3305 start = get_irn_node(initial_bl);
3306 add_to_cprop(start, &env);
3310 if (env.worklist != NULL)
3312 } while (env.cprop != NULL || env.worklist != NULL);
3314 dump_all_partitions(&env);
3315 check_all_partitions(&env);
3318 set_dump_node_vcgattr_hook(dump_partition_hook);
3319 dump_ir_block_graph(irg, "-partition");
3320 set_dump_node_vcgattr_hook(NULL);
3322 (void)dump_partition_hook;
3325 /* apply the result */
3326 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3327 irg_walk_graph(irg, NULL, apply_result, &env);
3328 apply_end(get_irg_end(irg), &env);
3331 /* control flow might changed */
3332 set_irg_outs_inconsistent(irg);
3333 set_irg_extblk_inconsistent(irg);
3334 set_irg_doms_inconsistent(irg);
3335 set_irg_loopinfo_inconsistent(irg);
3338 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
3340 pmap_destroy(env.type2id_map);
3341 del_set(env.opcode2id_map);
3342 obstack_free(&env.obst, NULL);
3344 /* restore value_of() default behavior */
3345 set_value_of_func(NULL);
3346 current_ir_graph = rem;