2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Scalar replacement of compounds.
9 * @author Beyhan Veliev, Michael Beck
16 #include "iroptimize.h"
17 #include "scalar_replace.h"
36 static unsigned get_vnum(const ir_node *node)
38 return (unsigned)PTR_TO_INT(get_irn_link(node));
41 static void set_vnum(ir_node *node, unsigned vnum)
43 set_irn_link(node, INT_TO_PTR(vnum));
47 * A path element entry: it is either an entity
48 * or a tarval, because we evaluate only constant array
49 * accesses like a.b.c[8].d
57 * An access path, used to assign value numbers
58 * to variables that will be scalar replaced.
60 typedef struct path_t {
61 unsigned vnum; /**< The value number. */
62 size_t path_len; /**< The length of the access path. */
63 path_elem_t path[1]; /**< The path. */
66 /** The size of a path in bytes. */
67 static size_t path_size(path_t *p)
69 return sizeof(*p) + sizeof(p->path[0]) * (p->path_len-1);
72 typedef struct scalars_t {
73 ir_entity *ent; /**< A entity for scalar replacement. */
76 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
81 * @return 0 if they are identically
83 static int path_cmp(const void *elt, const void *key, size_t size)
85 const path_t *p1 = (const path_t*)elt;
86 const path_t *p2 = (const path_t*)key;
89 /* we can use memcmp here, because identical tarvals should have identical addresses */
90 return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
94 * Compare two elements of the scalars_t set.
96 * @return 0 if they are identically
98 static int ent_cmp(const void *elt, const void *key, size_t size)
100 const scalars_t *c1 = (const scalars_t*)elt;
101 const scalars_t *c2 = (const scalars_t*)key;
104 return c1->ent != c2->ent;
108 * Calculate a hash value for a path.
110 static unsigned path_hash(const path_t *path)
115 for (i = 0; i < path->path_len; ++i)
116 hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
122 * Returns non-zero, if all indeces of a Sel node are constants.
124 * @param sel the Sel node that will be checked
126 static bool is_const_sel(ir_node *sel)
128 int i, n = get_Sel_n_indexs(sel);
130 for (i = 0; i < n; ++i) {
131 ir_node *idx = get_Sel_index(sel, i);
140 * Check the mode of a Load/Store with the mode of the entity
142 * If the mode of the entity and the Load/Store mode do not match, we
143 * have the bad reinterpret case:
146 * char b = *(char *)&i;
148 * We do NOT count this as one value and return address_taken
150 * However, we support an often used case. If the mode is two-complement
151 * we allow casts between signed/unsigned.
153 * @param mode the mode of the Load/Store
154 * @param ent_mode the mode of the accessed entity
156 static bool check_load_store_mode(ir_mode *mode, ir_mode *ent_mode)
158 if (ent_mode != mode) {
159 if (ent_mode == NULL ||
160 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
161 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
162 get_mode_arithmetic(mode) != irma_twos_complement)
169 * Returns non-zero, if the address of an entity
170 * represented by a Sel node (or its successor Sels) is taken.
172 bool is_address_taken(ir_node *sel)
175 ir_mode *emode, *mode;
179 if (! is_const_sel(sel))
182 for (unsigned i = get_irn_n_outs(sel); i-- > 0; ) {
183 ir_node *succ = get_irn_out(sel, i);
185 switch (get_irn_opcode(succ)) {
187 /* do not remove volatile variables */
188 if (get_Load_volatility(succ) == volatility_is_volatile)
190 /* check if this load is not a hidden conversion */
191 mode = get_Load_mode(succ);
192 ent = get_Sel_entity(sel);
193 emode = get_type_mode(get_entity_type(ent));
194 if (! check_load_store_mode(mode, emode))
199 /* check that Sel is not the Store's value */
200 value = get_Store_value(succ);
203 /* do not remove volatile variables */
204 if (get_Store_volatility(succ) == volatility_is_volatile)
206 /* check if this Store is not a hidden conversion */
207 mode = get_irn_mode(value);
208 ent = get_Sel_entity(sel);
209 emode = get_type_mode(get_entity_type(ent));
210 if (! check_load_store_mode(mode, emode))
216 ir_entity *entity = get_Sel_entity(succ);
217 /* we can't handle unions correctly yet -> address taken */
218 if (is_Union_type(get_entity_owner(entity)))
221 /* Check the Sel successor of Sel */
222 res = is_address_taken(succ);
229 /* The address of an entity is given as a parameter.
230 * As long as we do not have analyses that can tell what
231 * is done with parameters, think is taken.
232 * One special case: If the Call type tells that it's a
233 * value parameter, the address is NOT taken.
238 int res = is_address_taken(succ);
245 /* Non-optimized Tuple, happens in inlining */
246 for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0; --input_nr) {
247 ir_node *pred = get_Tuple_pred(succ, input_nr);
250 /* we found one input */
251 for (unsigned k = get_irn_n_outs(succ); k-- > 0; ) {
252 ir_node *proj = get_irn_out(succ, k);
254 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
255 int res = is_address_taken(proj);
265 /* another op, the address is taken */
273 * Link all leave Sels with the entity.
275 * @param ent the entity that will be scalar replaced
276 * @param sel a Sel node that selects some fields of this entity
278 static bool link_all_leave_sels(ir_entity *ent, ir_node *sel)
280 bool is_leave = true;
282 for (unsigned i = get_irn_n_outs(sel); i-- > 0; ) {
283 ir_node *succ = get_irn_out(sel, i);
286 /* the current leave has further Sel's, no leave */
288 link_all_leave_sels(ent, succ);
289 } else if (is_Id(succ)) {
290 is_leave &= link_all_leave_sels(ent, succ);
298 /* we know we are at a leave, because this function is only
299 * called if the address is NOT taken, so sel's successor(s)
300 * must be Loads or Stores
302 set_irn_link(sel, get_entity_link(ent));
303 set_entity_link(ent, sel);
308 /* we need a special address that serves as an address taken marker */
310 static void *ADDRESS_TAKEN = &_x;
313 * Find possible scalar replacements.
315 * @param irg an IR graph
317 * This function finds variables on the (members of the) frame type
318 * that can be scalar replaced, because their address is never taken.
319 * If such a variable is found, its entity link will hold a list of all
320 * Sel nodes, that selects the atomic fields of this entity.
321 * Otherwise, the link will be ADDRESS_TAKEN or NULL.
323 * @return non-zero if at least one entity could be replaced
326 static int find_possible_replacements(ir_graph *irg)
331 long static_link_arg;
335 * First, clear the link field of all interesting entities.
337 frame_tp = get_irg_frame_type(irg);
338 for (mem_idx = get_class_n_members(frame_tp); mem_idx > 0;) {
339 ir_entity *ent = get_class_member(frame_tp, --mem_idx);
340 set_entity_link(ent, NULL);
343 /* check for inner functions:
344 * FIXME: need a way to get the argument position for the static link */
346 for (mem_idx = get_class_n_members(frame_tp); mem_idx > 0;) {
347 ir_entity *ent = get_class_member(frame_tp, --mem_idx);
348 if (is_method_entity(ent)) {
349 ir_graph *inner_irg = get_entity_irg(ent);
352 assure_irg_properties(inner_irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
353 args = get_irg_args(inner_irg);
354 for (unsigned j = get_irn_n_outs(args); j-- > 0; ) {
355 ir_node *arg = get_irn_out(args, j);
357 if (get_Proj_proj(arg) == static_link_arg) {
358 for (unsigned k = get_irn_n_outs(arg); k-- > 0; ) {
359 ir_node *succ = get_irn_out(arg, k);
362 ir_entity *ent = get_Sel_entity(succ);
364 if (get_entity_owner(ent) == frame_tp) {
365 /* found an access to the outer frame */
366 set_entity_link(ent, ADDRESS_TAKEN);
376 * Check the ir_graph for Sel nodes. If the entity of Sel
377 * isn't a scalar replacement set the link of this entity
378 * equal ADDRESS_TAKEN.
380 irg_frame = get_irg_frame(irg);
381 for (unsigned i = get_irn_n_outs(irg_frame); i-- > 0; ) {
382 ir_node *succ = get_irn_out(irg_frame, i);
385 ir_entity *ent = get_Sel_entity(succ);
388 /* we are only interested in entities on the frame, NOT
390 if (get_entity_owner(ent) != frame_tp)
393 if (get_entity_link(ent) == ADDRESS_TAKEN)
396 ent_type = get_entity_type(ent);
398 /* we can handle arrays, structs and atomic types yet */
399 if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
400 if (is_address_taken(succ)) {
402 if (get_entity_link(ent))
404 set_entity_link(ent, ADDRESS_TAKEN);
406 /* possible found one */
407 if (get_entity_link(ent) == NULL)
409 link_all_leave_sels(ent, succ);
419 * Return a path from the Sel node "sel" to its root.
421 * @param sel the Sel node
422 * @param len the length of the path so far
424 static path_t *find_path(ir_node *sel, size_t len)
429 ir_node *pred = get_Sel_ptr(sel);
431 /* the current Sel node will add some path elements */
432 n = get_Sel_n_indexs(sel);
435 if (! is_Sel(pred)) {
436 /* we found the root */
437 res = XMALLOCF(path_t, path, len);
440 res = find_path(pred, len);
442 assert(len <= res->path_len);
443 pos = res->path_len - len;
445 res->path[pos++].ent = get_Sel_entity(sel);
446 for (i = 0; i < n; ++i) {
447 ir_node *index = get_Sel_index(sel, i);
449 res->path[pos++].tv = get_Const_tarval(index);
456 * Allocate value numbers for the leaves
457 * in our found entities.
459 * @param sels a set that will contain all Sels that have a value number
460 * @param ent the entity that will be scalar replaced
461 * @param vnum the first value number we can assign
462 * @param modes a flexible array, containing all the modes of
465 * @return the next free value number
467 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
471 set *pathes = new_set(path_cmp, 8);
473 DB((dbg, SET_LEVEL_3, " Visiting Sel nodes of entity %+F\n", ent));
474 /* visit all Sel nodes in the chain of the entity */
475 for (sel = (ir_node*)get_entity_link(ent); sel != NULL; sel = next) {
476 next = (ir_node*)get_irn_link(sel);
478 /* we must mark this sel for later */
479 pset_insert_ptr(sels, sel);
481 key = find_path(sel, 0);
482 path = set_find(path_t, pathes, key, path_size(key), path_hash(key));
485 set_vnum(sel, path->vnum);
486 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, path->vnum));
490 (void)set_insert(path_t, pathes, key, path_size(key), path_hash(key));
492 set_vnum(sel, key->vnum);
493 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, key->vnum));
495 ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15);
497 (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
499 assert((*modes)[key->vnum] && "Value is not atomic");
505 DB((dbg, SET_LEVEL_2, " %s", get_entity_name(key->path[0].ent)));
506 for (i = 1; i < key->path_len; ++i) {
507 if (is_entity(key->path[i].ent))
508 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
510 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
512 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
514 #endif /* DEBUG_libfirm */
520 set_entity_link(ent, NULL);
525 * environment for memory walker
527 typedef struct env_t {
528 unsigned nvals; /**< number of values */
529 ir_mode **modes; /**< the modes of the values */
530 pset *sels; /**< A set of all Sel nodes that have a value number */
534 * topological post-walker.
536 static void walker(ir_node *node, void *ctx)
538 env_t *env = (env_t*)ctx;
539 ir_graph *irg = get_irn_irg(node);
540 ir_node *addr, *block, *mem, *val;
545 /* a load, check if we can resolve it */
546 addr = get_Load_ptr(node);
548 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
549 if (! is_Sel(addr)) {
550 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", addr));
554 if (! pset_find_ptr(env->sels, addr)) {
555 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", addr));
559 /* ok, we have a Load that will be replaced */
560 vnum = get_vnum(addr);
561 assert(vnum < env->nvals);
563 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
565 block = get_nodes_block(node);
566 set_cur_block(block);
568 /* check, if we can replace this Load */
569 val = get_value(vnum, env->modes[vnum]);
571 /* Beware: A Load can contain a hidden conversion in Firm.
572 This happens for instance in the following code:
575 unsigned j = *(unsigned *)&i;
578 mode = get_Load_mode(node);
579 if (mode != get_irn_mode(val))
580 val = new_rd_Conv(get_irn_dbg_info(node), block, val, mode);
582 mem = get_Load_mem(node);
583 ir_node *const in[] = {
586 [pn_Load_X_regular] = new_r_Jmp(block),
587 [pn_Load_X_except] = new_r_Bad(irg, mode_X),
589 turn_into_tuple(node, ARRAY_SIZE(in), in);
590 } else if (is_Store(node)) {
591 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
593 /* a Store always can be replaced */
594 addr = get_Store_ptr(node);
596 if (! is_Sel(addr)) {
597 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", addr));
601 if (! pset_find_ptr(env->sels, addr)) {
602 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", addr));
606 vnum = get_vnum(addr);
607 assert(vnum < env->nvals);
609 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
611 block = get_nodes_block(node);
612 set_cur_block(block);
614 /* Beware: A Store can contain a hidden conversion in Firm. */
615 val = get_Store_value(node);
616 if (get_irn_mode(val) != env->modes[vnum])
617 val = new_rd_Conv(get_irn_dbg_info(node), block, val, env->modes[vnum]);
619 set_value(vnum, val);
621 mem = get_Store_mem(node);
622 ir_node *const in[] = {
624 [pn_Store_X_regular] = new_r_Jmp(block),
625 [pn_Store_X_except] = new_r_Bad(irg, mode_X),
627 turn_into_tuple(node, ARRAY_SIZE(in), in);
632 * Make scalar replacement.
634 * @param sels A set containing all Sel nodes that have a value number
635 * @param nvals The number of scalars.
636 * @param modes A flexible array, containing all the modes of
639 static void do_scalar_replacements(ir_graph *irg, pset *sels, unsigned nvals,
644 ssa_cons_start(irg, (int)nvals);
651 * second step: walk over the graph blockwise in topological order
652 * and fill the array as much as possible.
654 DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", irg));
655 irg_walk_blkwise_graph(irg, NULL, walker, &env);
657 ssa_cons_finish(irg);
661 * Find possible scalar replacements
663 * @param irg The current ir graph.
665 void scalar_replacement_opt(ir_graph *irg)
673 ir_type *ent_type, *frame_tp;
675 assure_irg_properties(irg,
676 IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
677 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
679 /* we use the link field to store the VNUM */
680 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
681 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
683 /* Find possible scalar replacements */
684 if (find_possible_replacements(irg)) {
685 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %+F\n", irg));
687 /* Insert in set the scalar replacements. */
688 irg_frame = get_irg_frame(irg);
690 modes = NEW_ARR_F(ir_mode *, 16);
691 set_ent = new_set(ent_cmp, 8);
692 sels = pset_new_ptr(8);
693 frame_tp = get_irg_frame_type(irg);
695 for (unsigned i = get_irn_n_outs(irg_frame); i-- > 0; ) {
696 ir_node *succ = get_irn_out(irg_frame, i);
699 ir_entity *ent = get_Sel_entity(succ);
701 /* we are only interested in entities on the frame, NOT
703 if (get_entity_owner(ent) != frame_tp
704 || is_parameter_entity(ent))
707 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
710 ent_type = get_entity_type(ent);
713 (void)set_insert(scalars_t, set_ent, &key, sizeof(key), hash_ptr(key.ent));
716 if (is_Array_type(ent_type)) {
717 DB((dbg, SET_LEVEL_1, " found array %s\n", get_entity_name(ent)));
718 } else if (is_Struct_type(ent_type)) {
719 DB((dbg, SET_LEVEL_1, " found struct %s\n", get_entity_name(ent)));
720 } else if (is_atomic_type(ent_type))
721 DB((dbg, SET_LEVEL_1, " found atomic value %s\n", get_entity_name(ent)));
723 panic("Neither an array nor a struct or atomic value found in scalar replace");
725 #endif /* DEBUG_libfirm */
727 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
731 DB((dbg, SET_LEVEL_1, " %u values will be needed\n", nvals));
733 /* If scalars were found. */
735 do_scalar_replacements(irg, sels, nvals, modes);
737 foreach_set(set_ent, scalars_t, value) {
738 free_entity(value->ent);
742 * We changed the graph, but did NOT introduce new blocks
743 * neither changed control flow, cf-backedges should be still
752 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
753 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
755 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
758 ir_graph_pass_t *scalar_replacement_opt_pass(const char *name)
760 return def_graph_pass(name ? name : "scalar_rep", scalar_replacement_opt);
763 void firm_init_scalar_replace(void)
765 FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");