2 * Copyright (C) 1995-2011 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 size_t 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 = (const path_t*)elt;
90 const path_t *p2 = (const path_t*)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 = (const scalars_t*)elt;
105 const scalars_t *c2 = (const scalars_t*)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, size_t len)
444 ir_node *pred = get_Sel_ptr(sel);
446 /* the current Sel node will add some path elements */
447 n = get_Sel_n_indexs(sel);
450 if (! is_Sel(pred)) {
451 /* we found the root */
452 res = XMALLOCF(path_t, path, len);
455 res = find_path(pred, len);
457 assert(len <= res->path_len);
458 pos = res->path_len - len;
460 res->path[pos++].ent = get_Sel_entity(sel);
461 for (i = 0; i < n; ++i) {
462 ir_node *index = get_Sel_index(sel, i);
464 res->path[pos++].tv = get_Const_tarval(index);
471 * Allocate value numbers for the leaves
472 * in our found entities.
474 * @param sels a set that will contain all Sels that have a value number
475 * @param ent the entity that will be scalar replaced
476 * @param vnum the first value number we can assign
477 * @param modes a flexible array, containing all the modes of
480 * @return the next free value number
482 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
486 set *pathes = new_set(path_cmp, 8);
488 DB((dbg, SET_LEVEL_3, " Visiting Sel nodes of entity %+F\n", ent));
489 /* visit all Sel nodes in the chain of the entity */
490 for (sel = (ir_node*)get_entity_link(ent); sel != NULL;
492 next = (ir_node*)get_irn_link(sel);
494 /* we must mark this sel for later */
495 pset_insert_ptr(sels, sel);
497 key = find_path(sel, 0);
498 path = (path_t*)set_find(pathes, key, PATH_SIZE(key), path_hash(key));
501 SET_VNUM(sel, path->vnum);
502 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, path->vnum));
506 set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
508 SET_VNUM(sel, key->vnum);
509 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, key->vnum));
511 ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15);
513 (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
515 assert((*modes)[key->vnum] && "Value is not atomic");
521 DB((dbg, SET_LEVEL_2, " %s", get_entity_name(key->path[0].ent)));
522 for (i = 1; i < key->path_len; ++i) {
523 if (is_entity(key->path[i].ent))
524 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
526 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
528 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
530 #endif /* DEBUG_libfirm */
536 set_entity_link(ent, NULL);
541 * A list entry for the fixing lists
543 typedef struct list_entry_t {
544 ir_node *node; /**< the node that must be fixed */
545 unsigned vnum; /**< the value number of this node */
549 * environment for memory walker
551 typedef struct env_t {
552 int nvals; /**< number of values */
553 ir_mode **modes; /**< the modes of the values */
554 pset *sels; /**< A set of all Sel nodes that have a value number */
558 * topological post-walker.
560 static void topologic_walker(ir_node *node, void *ctx)
562 env_t *env = (env_t*)ctx;
563 ir_graph *irg = get_irn_irg(node);
564 ir_node *adr, *block, *mem, *val;
569 /* a load, check if we can resolve it */
570 adr = get_Load_ptr(node);
572 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
574 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
578 if (! pset_find_ptr(env->sels, adr)) {
579 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
583 /* ok, we have a Load that will be replaced */
584 vnum = GET_VNUM(adr);
585 assert(vnum < (unsigned)env->nvals);
587 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
589 block = get_nodes_block(node);
590 set_cur_block(block);
592 /* check, if we can replace this Load */
593 val = get_value(vnum, env->modes[vnum]);
595 /* Beware: A Load can contain a hidden conversion in Firm.
596 This happens for instance in the following code:
599 unsigned j = *(unsigned *)&i;
602 mode = get_Load_mode(node);
603 if (mode != get_irn_mode(val))
604 val = new_rd_Conv(get_irn_dbg_info(node), block, val, mode);
606 mem = get_Load_mem(node);
607 turn_into_tuple(node, pn_Load_max);
608 set_Tuple_pred(node, pn_Load_M, mem);
609 set_Tuple_pred(node, pn_Load_res, val);
610 set_Tuple_pred(node, pn_Load_X_regular, new_r_Jmp(block));
611 set_Tuple_pred(node, pn_Load_X_except, new_r_Bad(irg));
612 } else if (is_Store(node)) {
613 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
615 /* a Store always can be replaced */
616 adr = get_Store_ptr(node);
619 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
623 if (! pset_find_ptr(env->sels, adr)) {
624 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
628 vnum = GET_VNUM(adr);
629 assert(vnum < (unsigned)env->nvals);
631 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
633 block = get_nodes_block(node);
634 set_cur_block(block);
636 /* Beware: A Store can contain a hidden conversion in Firm. */
637 val = get_Store_value(node);
638 if (get_irn_mode(val) != env->modes[vnum])
639 val = new_rd_Conv(get_irn_dbg_info(node), block, val, env->modes[vnum]);
641 set_value(vnum, val);
643 mem = get_Store_mem(node);
644 turn_into_tuple(node, pn_Store_max);
645 set_Tuple_pred(node, pn_Store_M, mem);
646 set_Tuple_pred(node, pn_Store_X_regular, new_r_Jmp(block));
647 set_Tuple_pred(node, pn_Store_X_except, new_r_Bad(irg));
652 * Make scalar replacement.
654 * @param sels A set containing all Sel nodes that have a value number
655 * @param nvals The number of scalars.
656 * @param modes A flexible array, containing all the modes of
659 static void do_scalar_replacements(ir_graph *irg, pset *sels, int nvals,
664 ssa_cons_start(irg, nvals);
671 * second step: walk over the graph blockwise in topological order
672 * and fill the array as much as possible.
674 DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", irg));
675 irg_walk_blkwise_graph(irg, NULL, topologic_walker, &env);
677 ssa_cons_finish(irg);
681 * Find possible scalar replacements
683 * @param irg The current ir graph.
685 int scalar_replacement_opt(ir_graph *irg)
689 scalars_t key, *value;
694 ir_type *ent_type, *frame_tp;
697 /* Call algorithm that computes the out edges */
698 assure_irg_outs(irg);
700 /* we use the link field to store the VNUM */
701 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
702 irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
704 /* Find possible scalar replacements */
705 if (find_possible_replacements(irg)) {
706 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %+F\n", irg));
708 /* Insert in set the scalar replacements. */
709 irg_frame = get_irg_frame(irg);
711 modes = NEW_ARR_F(ir_mode *, 16);
712 set_ent = new_set(ent_cmp, 8);
713 sels = pset_new_ptr(8);
714 frame_tp = get_irg_frame_type(irg);
716 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
717 ir_node *succ = get_irn_out(irg_frame, i);
720 ir_entity *ent = get_Sel_entity(succ);
722 /* we are only interested in entities on the frame, NOT
724 if (get_entity_owner(ent) != frame_tp)
727 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
730 ent_type = get_entity_type(ent);
733 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
736 if (is_Array_type(ent_type)) {
737 DB((dbg, SET_LEVEL_1, " found array %s\n", get_entity_name(ent)));
738 } else if (is_Struct_type(ent_type)) {
739 DB((dbg, SET_LEVEL_1, " found struct %s\n", get_entity_name(ent)));
740 } else if (is_atomic_type(ent_type))
741 DB((dbg, SET_LEVEL_1, " found atomic value %s\n", get_entity_name(ent)));
743 panic("Neither an array nor a struct or atomic value found in scalar replace");
745 #endif /* DEBUG_libfirm */
747 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
751 DB((dbg, SET_LEVEL_1, " %u values will be needed\n", nvals));
753 /* If scalars were found. */
755 do_scalar_replacements(irg, sels, nvals, modes);
757 foreach_set(set_ent, scalars_t*, value) {
758 free_entity(value->ent);
762 * We changed the graph, but did NOT introduce new blocks
763 * neither changed control flow, cf-backedges should be still
766 set_irg_outs_inconsistent(irg);
767 set_irg_loopinfo_inconsistent(irg);
776 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
777 irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
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");