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 Scalar replacement of compounds.
23 * @author Beyhan Veliev, Michael Beck
30 #include "iroptimize.h"
31 #include "scalar_replace.h"
50 #define SET_VNUM(node, vnum) set_irn_link(node, INT_TO_PTR(vnum))
51 #define GET_VNUM(node) (unsigned)PTR_TO_INT(get_irn_link(node))
54 * A path element entry: it is either an entity
55 * or a tarval, because we evaluate only constant array
56 * accesses like a.b.c[8].d
64 * An access path, used to assign value numbers
65 * to variables that will be scalar replaced.
67 typedef struct path_t {
68 unsigned vnum; /**< The value number. */
69 unsigned path_len; /**< The length of the access path. */
70 path_elem_t path[1]; /**< The path. */
73 /** The size of a path in bytes. */
74 #define PATH_SIZE(p) (sizeof(*(p)) + sizeof((p)->path[0]) * ((p)->path_len - 1))
76 typedef struct scalars_t {
77 ir_entity *ent; /**< A entity for scalar replacement. */
80 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
85 * @return 0 if they are identically
87 static int path_cmp(const void *elt, const void *key, size_t size)
89 const path_t *p1 = elt;
90 const path_t *p2 = key;
93 /* we can use memcmp here, because identical tarvals should have identical addresses */
94 return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
98 * Compare two elements of the scalars_t set.
100 * @return 0 if they are identically
102 static int ent_cmp(const void *elt, const void *key, size_t size)
104 const scalars_t *c1 = elt;
105 const scalars_t *c2 = key;
108 return c1->ent != c2->ent;
112 * Calculate a hash value for a path.
114 static unsigned path_hash(const path_t *path)
119 for (i = 0; i < path->path_len; ++i)
120 hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
126 * Returns non-zero, if all indeces of a Sel node are constants.
128 * @param sel the Sel node that will be checked
130 static int is_const_sel(ir_node *sel)
132 int i, n = get_Sel_n_indexs(sel);
134 for (i = 0; i < n; ++i) {
135 ir_node *idx = get_Sel_index(sel, i);
144 * Check the mode of a Load/Store with the mode of the entity
146 * If the mode of the entity and the Load/Store mode do not match, we
147 * have the bad reinterpret case:
150 * char b = *(char *)&i;
152 * We do NOT count this as one value and return address_taken
154 * However, we support an often used case. If the mode is two-complement
155 * we allow casts between signed/unsigned.
157 * @param mode the mode of the Load/Store
158 * @param ent_mode the mode of the accessed entity
160 static int check_load_store_mode(ir_mode *mode, ir_mode *ent_mode)
162 if (ent_mode != mode) {
163 if (ent_mode == NULL ||
164 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
165 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
166 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
167 get_mode_arithmetic(mode) != irma_twos_complement)
174 * Returns non-zero, if the address of an entity
175 * represented by a Sel node (or it's successor Sels) is taken.
177 int is_address_taken(ir_node *sel)
180 ir_mode *emode, *mode;
184 if (! is_const_sel(sel))
187 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
188 ir_node *succ = get_irn_out(sel, i);
190 switch (get_irn_opcode(succ)) {
192 /* do not remove volatile variables */
193 if (get_Load_volatility(succ) == volatility_is_volatile)
195 /* check if this load is not a hidden conversion */
196 mode = get_Load_mode(succ);
197 ent = get_Sel_entity(sel);
198 emode = get_type_mode(get_entity_type(ent));
199 if (! check_load_store_mode(mode, emode))
204 /* check that Sel is not the Store's value */
205 value = get_Store_value(succ);
208 /* do not remove volatile variables */
209 if (get_Store_volatility(succ) == volatility_is_volatile)
211 /* check if this Store is not a hidden conversion */
212 mode = get_irn_mode(value);
213 ent = get_Sel_entity(sel);
214 emode = get_type_mode(get_entity_type(ent));
215 if (! check_load_store_mode(mode, emode))
221 ir_entity *entity = get_Sel_entity(succ);
222 /* we can't handle unions correctly yet -> address taken */
223 if (is_Union_type(get_entity_owner(entity)))
226 /* Check the Sel successor of Sel */
227 res = is_address_taken(succ);
234 /* The address of an entity is given as a parameter.
235 * As long as we do not have analyses that can tell what
236 * is done with parameters, think is taken.
237 * One special case: If the Call type tells that it's a
238 * value parameter, the address is NOT taken.
243 int res = is_address_taken(succ);
250 /* Non-optimized Tuple, happens in inlining */
251 for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0; --input_nr) {
252 ir_node *pred = get_Tuple_pred(succ, input_nr);
255 /* we found one input */
256 for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
257 ir_node *proj = get_irn_out(succ, k);
259 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
260 int res = is_address_taken(proj);
270 /* another op, the address is taken */
278 * Link all leave Sels with the entity.
280 * @param ent the entity that will be scalar replaced
281 * @param sel a Sel node that selects some fields of this entity
283 static int link_all_leave_sels(ir_entity *ent, ir_node *sel)
287 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
288 ir_node *succ = get_irn_out(sel, i);
291 /* the current leave has further Sel's, no leave */
293 link_all_leave_sels(ent, succ);
294 } else if (is_Id(succ)) {
295 is_leave &= link_all_leave_sels(ent, succ);
303 /* we know we are at a leave, because this function is only
304 * called if the address is NOT taken, so sel's successor(s)
305 * must be Loads or Stores
307 set_irn_link(sel, get_entity_link(ent));
308 set_entity_link(ent, sel);
313 /* we need a special address that serves as an address taken marker */
315 static void *ADDRESS_TAKEN = &_x;
318 * Find possible scalar replacements.
320 * @param irg an IR graph
322 * This function finds variables on the (members of the) frame type
323 * that can be scalar replaced, because their address is never taken.
324 * If such a variable is found, it's entity link will hold a list of all
325 * Sel nodes, that selects the atomic fields of this entity.
326 * Otherwise, the link will be ADDRESS_TAKEN or NULL.
328 * @return non-zero if at least one entity could be replaced
331 static int find_possible_replacements(ir_graph *irg)
335 int i, j, k, static_link_arg;
339 * First, clear the link field of all interesting entities.
341 frame_tp = get_irg_frame_type(irg);
342 for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
343 ir_entity *ent = get_class_member(frame_tp, i);
344 set_entity_link(ent, NULL);
347 /* check for inner functions:
348 * FIXME: need a way to get the argument position for the static link */
350 for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
351 ir_entity *ent = get_class_member(frame_tp, i);
352 if (is_method_entity(ent)) {
353 ir_graph *inner_irg = get_entity_irg(ent);
356 assure_irg_outs(inner_irg);
357 args = get_irg_args(inner_irg);
358 for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
359 ir_node *arg = get_irn_out(args, j);
361 if (get_Proj_proj(arg) == static_link_arg) {
362 for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
363 ir_node *succ = get_irn_out(arg, k);
366 ir_entity *ent = get_Sel_entity(succ);
368 if (get_entity_owner(ent) == frame_tp) {
369 /* found an access to the outer frame */
370 set_entity_link(ent, ADDRESS_TAKEN);
380 * Check the ir_graph for Sel nodes. If the entity of Sel
381 * isn't a scalar replacement set the link of this entity
382 * equal ADDRESS_TAKEN.
384 irg_frame = get_irg_frame(irg);
385 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
386 ir_node *succ = get_irn_out(irg_frame, i);
389 ir_entity *ent = get_Sel_entity(succ);
392 /* we are only interested in entities on the frame, NOT
394 if (get_entity_owner(ent) != frame_tp)
397 if (get_entity_link(ent) == ADDRESS_TAKEN)
401 * Beware: in rare cases even entities on the frame might be
402 * volatile. This might happen if the entity serves as a store
403 * to a value that must survive a exception. Do not optimize
404 * such entities away.
406 if (get_entity_volatility(ent) == volatility_is_volatile) {
407 set_entity_link(ent, ADDRESS_TAKEN);
411 ent_type = get_entity_type(ent);
413 /* we can handle arrays, structs and atomic types yet */
414 if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
415 if (is_address_taken(succ)) {
417 if (get_entity_link(ent))
419 set_entity_link(ent, ADDRESS_TAKEN);
421 /* possible found one */
422 if (get_entity_link(ent) == NULL)
424 link_all_leave_sels(ent, succ);
434 * Return a path from the Sel node sel to it's root.
436 * @param sel the Sel node
437 * @param len the length of the path so far
439 static path_t *find_path(ir_node *sel, unsigned len)
443 ir_node *pred = get_Sel_ptr(sel);
445 /* the current Sel node will add some path elements */
446 n = get_Sel_n_indexs(sel);
449 if (! is_Sel(pred)) {
450 /* we found the root */
451 res = XMALLOCF(path_t, path, len);
454 res = find_path(pred, len);
456 pos = res->path_len - len;
458 res->path[pos++].ent = get_Sel_entity(sel);
459 for (i = 0; i < n; ++i) {
460 ir_node *index = get_Sel_index(sel, i);
462 res->path[pos++].tv = get_Const_tarval(index);
469 * Allocate value numbers for the leaves
470 * in our found entities.
472 * @param sels a set that will contain all Sels that have a value number
473 * @param ent the entity that will be scalar replaced
474 * @param vnum the first value number we can assign
475 * @param modes a flexible array, containing all the modes of
478 * @return the next free value number
480 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
484 set *pathes = new_set(path_cmp, 8);
486 DB((dbg, SET_LEVEL_3, " Visiting Sel nodes of entity %+F\n", ent));
487 /* visit all Sel nodes in the chain of the entity */
488 for (sel = get_entity_link(ent); sel; sel = next) {
489 next = get_irn_link(sel);
491 /* we must mark this sel for later */
492 pset_insert_ptr(sels, sel);
494 key = find_path(sel, 0);
495 path = set_find(pathes, key, PATH_SIZE(key), path_hash(key));
498 SET_VNUM(sel, path->vnum);
499 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, path->vnum));
503 set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
505 SET_VNUM(sel, key->vnum);
506 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, key->vnum));
508 ARR_EXTO(ir_mode *, *modes, (int)((key->vnum + 15) & ~15));
510 (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
512 assert((*modes)[key->vnum] && "Value is not atomic");
518 DB((dbg, SET_LEVEL_2, " %s", get_entity_name(key->path[0].ent)));
519 for (i = 1; i < key->path_len; ++i) {
520 if (is_entity(key->path[i].ent))
521 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
523 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
525 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
527 #endif /* DEBUG_libfirm */
533 set_entity_link(ent, NULL);
538 * A list entry for the fixing lists
540 typedef struct list_entry_t {
541 ir_node *node; /**< the node that must be fixed */
542 unsigned vnum; /**< the value number of this node */
546 * environment for memory walker
548 typedef struct env_t {
549 int nvals; /**< number of values */
550 ir_mode **modes; /**< the modes of the values */
551 pset *sels; /**< A set of all Sel nodes that have a value number */
555 * topological post-walker.
557 static void topologic_walker(ir_node *node, void *ctx)
560 ir_node *adr, *block, *mem, *val;
565 /* a load, check if we can resolve it */
566 adr = get_Load_ptr(node);
568 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
570 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
574 if (! pset_find_ptr(env->sels, adr)) {
575 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
579 /* ok, we have a Load that will be replaced */
580 vnum = GET_VNUM(adr);
581 assert(vnum < (unsigned)env->nvals);
583 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
585 block = get_nodes_block(node);
586 set_cur_block(block);
588 /* check, if we can replace this Load */
589 val = get_value(vnum, env->modes[vnum]);
591 /* Beware: A Load can contain a hidden conversion in Firm.
592 This happens for instance in the following code:
595 unsigned j = *(unsigned *)&i;
598 mode = get_Load_mode(node);
599 if (mode != get_irn_mode(val))
600 val = new_d_Conv(get_irn_dbg_info(node), val, mode);
602 mem = get_Load_mem(node);
603 turn_into_tuple(node, pn_Load_max);
604 set_Tuple_pred(node, pn_Load_M, mem);
605 set_Tuple_pred(node, pn_Load_res, val);
606 set_Tuple_pred(node, pn_Load_X_regular, new_Jmp());
607 set_Tuple_pred(node, pn_Load_X_except, new_Bad());
608 } else if (is_Store(node)) {
609 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
611 /* a Store always can be replaced */
612 adr = get_Store_ptr(node);
615 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
619 if (! pset_find_ptr(env->sels, adr)) {
620 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
624 vnum = GET_VNUM(adr);
625 assert(vnum < (unsigned)env->nvals);
627 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
629 block = get_nodes_block(node);
630 set_cur_block(block);
632 /* Beware: A Store can contain a hidden conversion in Firm. */
633 val = get_Store_value(node);
634 if (get_irn_mode(val) != env->modes[vnum])
635 val = new_d_Conv(get_irn_dbg_info(node), val, env->modes[vnum]);
637 set_value(vnum, val);
639 mem = get_Store_mem(node);
640 turn_into_tuple(node, pn_Store_max);
641 set_Tuple_pred(node, pn_Store_M, mem);
642 set_Tuple_pred(node, pn_Store_X_regular, new_Jmp());
643 set_Tuple_pred(node, pn_Store_X_except, new_Bad());
648 * Make scalar replacement.
650 * @param sels A set containing all Sel nodes that have a value number
651 * @param nvals The number of scalars.
652 * @param modes A flexible array, containing all the modes of
655 static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes)
659 ssa_cons_start(current_ir_graph, nvals);
666 * second step: walk over the graph blockwise in topological order
667 * and fill the array as much as possible.
669 DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", current_ir_graph));
670 irg_walk_blkwise_graph(current_ir_graph, NULL, topologic_walker, &env);
672 ssa_cons_finish(current_ir_graph);
676 * Find possible scalar replacements
678 * @param irg The current ir graph.
680 int scalar_replacement_opt(ir_graph *irg)
684 scalars_t key, *value;
689 ir_type *ent_type, *frame_tp;
693 rem = current_ir_graph;
694 current_ir_graph = irg;
696 /* Call algorithm that computes the out edges */
697 assure_irg_outs(irg);
699 /* we use the link field to store the VNUM */
700 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
701 irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
703 /* Find possible scalar replacements */
704 if (find_possible_replacements(irg)) {
705 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %+F\n", irg));
707 /* Insert in set the scalar replacements. */
708 irg_frame = get_irg_frame(irg);
710 modes = NEW_ARR_F(ir_mode *, 16);
711 set_ent = new_set(ent_cmp, 8);
712 sels = pset_new_ptr(8);
713 frame_tp = get_irg_frame_type(irg);
715 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
716 ir_node *succ = get_irn_out(irg_frame, i);
719 ir_entity *ent = get_Sel_entity(succ);
721 /* we are only interested in entities on the frame, NOT
723 if (get_entity_owner(ent) != frame_tp)
726 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
729 ent_type = get_entity_type(ent);
732 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
735 if (is_Array_type(ent_type)) {
736 DB((dbg, SET_LEVEL_1, " found array %s\n", get_entity_name(ent)));
737 } else if (is_Struct_type(ent_type)) {
738 DB((dbg, SET_LEVEL_1, " found struct %s\n", get_entity_name(ent)));
739 } else if (is_atomic_type(ent_type))
740 DB((dbg, SET_LEVEL_1, " found atomic value %s\n", get_entity_name(ent)));
742 panic("Neither an array nor a struct or atomic value found in scalar replace");
744 #endif /* DEBUG_libfirm */
746 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
750 DB((dbg, SET_LEVEL_1, " %u values will be needed\n", nvals));
752 /* If scalars were found. */
754 do_scalar_replacements(sels, nvals, modes);
756 foreach_set(set_ent, value) {
757 free_entity(value->ent);
761 * We changed the graph, but did NOT introduce new blocks
762 * neither changed control flow, cf-backedges should be still
765 set_irg_outs_inconsistent(irg);
766 set_irg_loopinfo_inconsistent(irg);
775 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
776 irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
778 current_ir_graph = rem;
782 ir_graph_pass_t *scalar_replacement_opt_pass(const char *name)
784 return def_graph_pass_ret(name ? name : "scalar_rep",
785 scalar_replacement_opt);
788 void firm_init_scalar_replace(void)
790 FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");