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"
48 #define SET_VNUM(node, vnum) set_irn_link(node, INT_TO_PTR(vnum))
49 #define GET_VNUM(node) (unsigned)PTR_TO_INT(get_irn_link(node))
52 * A path element entry: it is either an entity
53 * or a tarval, because we evaluate only constant array
54 * accesses like a.b.c[8].d
62 * An access path, used to assign value numbers
63 * to variables that will be scalar replaced.
65 typedef struct _path_t {
66 unsigned vnum; /**< The value number. */
67 unsigned path_len; /**< The length of the access path. */
68 path_elem_t path[1]; /**< The path. */
71 /** The size of a path in bytes. */
72 #define PATH_SIZE(p) (sizeof(*(p)) + sizeof((p)->path[0]) * ((p)->path_len - 1))
74 typedef struct _scalars_t {
75 ir_entity *ent; /**< A entity for scalar replacement. */
76 ir_type *ent_owner; /**< The owner of this entity. */
79 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
84 * @return 0 if they are identically
86 static int path_cmp(const void *elt, const void *key, size_t size) {
87 const path_t *p1 = elt;
88 const path_t *p2 = key;
91 /* we can use memcmp here, because identical tarvals should have identical addresses */
92 return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
96 * Compare two elements of the scalars_t set.
98 * @return 0 if they are identically
100 static int ent_cmp(const void *elt, const void *key, size_t size) {
101 const scalars_t *c1 = elt;
102 const scalars_t *c2 = key;
105 return c1->ent != c2->ent;
109 * Calculate a hash value for a path.
111 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 int is_const_sel(ir_node *sel) {
127 int i, n = get_Sel_n_indexs(sel);
129 for (i = 0; i < n; ++i) {
130 ir_node *idx = get_Sel_index(sel, i);
139 * Check the mode of a Load/Store with the mode of the entity
141 * If the mode of the entity and the Load/Store mode do not match, we
142 * have the bad reinterpret case:
145 * char b = *(char *)&i;
147 * We do NOT count this as one value and return address_taken
149 * However, we support an often used case. If the mode is two-complement
150 * we allow casts between signed/unsigned.
152 * @param mode the mode of the Load/Store
153 * @param ent_mode the mode of the accessed entity
155 static int check_load_store_mode(ir_mode *mode, ir_mode *ent_mode) {
156 if (ent_mode != mode) {
157 if (ent_mode == NULL ||
158 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
159 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
160 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
161 get_mode_arithmetic(mode) != irma_twos_complement)
168 * Returns non-zero, if the address of an entity
169 * represented by a Sel node (or it's successor Sels) is taken.
171 int is_address_taken(ir_node *sel)
174 ir_mode *emode, *mode;
178 if (! is_const_sel(sel))
181 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
182 ir_node *succ = get_irn_out(sel, i);
184 switch (get_irn_opcode(succ)) {
186 /* do not remove volatile variables */
187 if (get_Load_volatility(succ) == volatility_is_volatile)
189 /* check if this load is not a hidden conversion */
190 mode = get_Load_mode(succ);
191 ent = get_Sel_entity(sel);
192 emode = get_type_mode(get_entity_type(ent));
193 if (! check_load_store_mode(mode, emode))
198 /* check that Sel is not the Store's value */
199 value = get_Store_value(succ);
202 /* do not remove volatile variables */
203 if (get_Store_volatility(succ) == volatility_is_volatile)
205 /* check if this Store is not a hidden conversion */
206 mode = get_irn_mode(value);
207 ent = get_Sel_entity(sel);
208 emode = get_type_mode(get_entity_type(ent));
209 if (! check_load_store_mode(mode, emode))
214 /* Check the Sel successor of Sel */
215 int res = is_address_taken(succ);
223 /* The address of an entity is given as a parameter.
224 * As long as we do not have analyses that can tell what
225 * is done with parameters, think is taken.
226 * One special case: If the Call type tells that it's a
227 * value parameter, the address is NOT taken.
232 int res = is_address_taken(succ);
239 /* Non-optimized Tuple, happens in inlining */
240 for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0; --input_nr) {
241 ir_node *pred = get_Tuple_pred(succ, input_nr);
244 /* we found one input */
245 for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
246 ir_node *proj = get_irn_out(succ, k);
248 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
249 int res = is_address_taken(proj);
259 /* another op, the address is taken */
267 * Link all leave Sels with the entity.
269 * @param ent the entity that will be scalar replaced
270 * @param sel a Sel node that selects some fields of this entity
272 * Uses the visited flag to mark already linked Sel nodes.
274 static void link_all_leave_sels(ir_entity *ent, ir_node *sel) {
277 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
278 ir_node *succ = get_irn_out(sel, i);
281 link_all_leave_sels(ent, succ);
287 /* if Sel nodes with memory inputs are used, a entity can be
288 * visited more than once causing a ring here, so we use the
289 * node flag to mark linked nodes
291 if (irn_visited_else_mark(sel))
294 /* we know we are at a leave, because this function is only
295 * called if the address is NOT taken, so succ must be a Load
298 set_irn_link(sel, get_entity_link(ent));
299 set_entity_link(ent, sel);
303 /* we need a special address that serves as an address taken marker */
305 static void *ADDRESS_TAKEN = &_x;
308 * Find possible scalar replacements.
310 * @param irg an IR graph
312 * This function finds variables on the (members of the) frame type
313 * that can be scalar replaced, because their address is never taken.
314 * If such a variable is found, it's entity link will hold a list of all
315 * Sel nodes, that selects the atomic fields of this entity.
316 * Otherwise, the link will be ADDRESS_TAKEN or NULL.
318 * @return non-zero if at least one entity could be replaced
321 static int find_possible_replacements(ir_graph *irg) {
327 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
328 inc_irg_visited(irg);
331 * First, clear the link field of all interesting entities.
333 frame_tp = get_irg_frame_type(irg);
334 for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
335 ir_entity *ent = get_class_member(frame_tp, i);
336 set_entity_link(ent, NULL);
340 * Check the ir_graph for Sel nodes. If the entity of Sel
341 * isn't a scalar replacement set the link of this entity
342 * equal ADDRESS_TAKEN.
344 irg_frame = get_irg_frame(irg);
345 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
346 ir_node *succ = get_irn_out(irg_frame, i);
349 ir_entity *ent = get_Sel_entity(succ);
352 if (get_entity_link(ent) == ADDRESS_TAKEN)
356 * Beware: in rare cases even entities on the frame might be
357 * volatile. This might happen if the entity serves as a store
358 * to a value that must survive a exception. Do not optimize
359 * such entities away.
361 if (get_entity_volatility(ent) == volatility_is_volatile) {
362 set_entity_link(ent, ADDRESS_TAKEN);
366 ent_type = get_entity_type(ent);
368 /* we can handle arrays, structs and atomic types yet */
369 if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
370 if (is_address_taken(succ)) {
371 if (get_entity_link(ent)) /* killing one */
373 set_entity_link(ent, ADDRESS_TAKEN);
375 /* possible found one */
376 if (get_entity_link(ent) == NULL)
378 link_all_leave_sels(ent, succ);
384 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
389 * Return a path from the Sel node sel to it's root.
391 * @param sel the Sel node
392 * @param len the length of the path so far
394 static path_t *find_path(ir_node *sel, unsigned len) {
397 ir_node *pred = get_Sel_ptr(sel);
399 /* the current Sel node will add some path elements */
400 n = get_Sel_n_indexs(sel);
403 if (! is_Sel(pred)) {
404 /* we found the root */
405 res = XMALLOCF(path_t, path, len);
408 res = find_path(pred, len);
410 pos = res->path_len - len;
412 res->path[pos++].ent = get_Sel_entity(sel);
413 for (i = 0; i < n; ++i) {
414 ir_node *index = get_Sel_index(sel, i);
416 res->path[pos++].tv = get_Const_tarval(index);
423 * Allocate value numbers for the leaves
424 * in our found entities.
426 * @param sels a set that will contain all Sels that have a value number
427 * @param ent the entity that will be scalar replaced
428 * @param vnum the first value number we can assign
429 * @param modes a flexible array, containing all the modes of
432 * @return the next free value number
434 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
438 set *pathes = new_set(path_cmp, 8);
440 DB((dbg, SET_LEVEL_3, " Visiting Sel nodes of entity %+F\n", ent));
441 /* visit all Sel nodes in the chain of the entity */
442 for (sel = get_entity_link(ent); sel; sel = next) {
443 next = get_irn_link(sel);
445 /* we must mark this sel for later */
446 pset_insert_ptr(sels, sel);
448 key = find_path(sel, 0);
449 path = set_find(pathes, key, PATH_SIZE(key), path_hash(key));
452 SET_VNUM(sel, path->vnum);
453 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, path->vnum));
457 set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
459 SET_VNUM(sel, key->vnum);
460 DB((dbg, SET_LEVEL_3, " %+F represents value %u\n", sel, key->vnum));
462 ARR_EXTO(ir_mode *, *modes, (int)((key->vnum + 15) & ~15));
464 (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
466 assert((*modes)[key->vnum] && "Value is not atomic");
472 DB((dbg, SET_LEVEL_2, " %s", get_entity_name(key->path[0].ent)));
473 for (i = 1; i < key->path_len; ++i) {
474 if (is_entity(key->path[i].ent))
475 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
477 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
479 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
481 #endif /* DEBUG_libfirm */
487 set_entity_link(ent, NULL);
492 * A list entry for the fixing lists
494 typedef struct _list_entry_t {
495 ir_node *node; /**< the node that must be fixed */
496 unsigned vnum; /**< the value number of this node */
500 * environment for memory walker
502 typedef struct _env_t {
503 int nvals; /**< number of values */
504 ir_mode **modes; /**< the modes of the values */
505 pset *sels; /**< A set of all Sel nodes that have a value number */
509 * topological post-walker.
511 static void topologic_walker(ir_node *node, void *ctx) {
513 ir_node *adr, *block, *mem, *val;
518 /* a load, check if we can resolve it */
519 adr = get_Load_ptr(node);
521 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
523 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
527 if (! pset_find_ptr(env->sels, adr)) {
528 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
532 /* ok, we have a Load that will be replaced */
533 vnum = GET_VNUM(adr);
534 assert(vnum < (unsigned)env->nvals);
536 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
538 block = get_nodes_block(node);
539 set_cur_block(block);
541 /* check, if we can replace this Load */
542 val = get_value(vnum, env->modes[vnum]);
544 /* Beware: A Load can contain a hidden conversion in Firm.
545 This happens for instance in the following code:
548 unsigned j = *(unsigned *)&i;
551 mode = get_Load_mode(node);
552 if (mode != get_irn_mode(val))
553 val = new_d_Conv(get_irn_dbg_info(node), val, mode);
555 mem = get_Load_mem(node);
556 turn_into_tuple(node, pn_Load_max);
557 set_Tuple_pred(node, pn_Load_M, mem);
558 set_Tuple_pred(node, pn_Load_res, val);
559 set_Tuple_pred(node, pn_Load_X_regular, new_Jmp());
560 set_Tuple_pred(node, pn_Load_X_except, new_Bad());
561 } else if (is_Store(node)) {
562 DB((dbg, SET_LEVEL_3, " checking %+F for replacement ", node));
564 /* a Store always can be replaced */
565 adr = get_Store_ptr(node);
568 DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
572 if (! pset_find_ptr(env->sels, adr)) {
573 DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
577 vnum = GET_VNUM(adr);
578 assert(vnum < (unsigned)env->nvals);
580 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
582 /* Beware: A Store can contain a hidden conversion in Firm. */
583 val = get_Store_value(node);
584 if (get_irn_mode(val) != env->modes[vnum])
585 val = new_d_Conv(get_irn_dbg_info(node), val, env->modes[vnum]);
587 block = get_nodes_block(node);
588 set_cur_block(block);
589 set_value(vnum, val);
591 mem = get_Store_mem(node);
592 turn_into_tuple(node, pn_Store_max);
593 set_Tuple_pred(node, pn_Store_M, mem);
594 set_Tuple_pred(node, pn_Store_X_regular, new_Jmp());
595 set_Tuple_pred(node, pn_Store_X_except, new_Bad());
600 * Make scalar replacement.
602 * @param sels A set containing all Sel nodes that have a value number
603 * @param nvals The number of scalars.
604 * @param modes A flexible array, containing all the modes of
607 static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes) {
610 ssa_cons_start(current_ir_graph, nvals);
617 * second step: walk over the graph blockwise in topological order
618 * and fill the array as much as possible.
620 DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", current_ir_graph));
621 irg_walk_blkwise_graph(current_ir_graph, NULL, topologic_walker, &env);
623 ssa_cons_finish(current_ir_graph);
627 * Find possible scalar replacements
629 * @param irg The current ir graph.
631 int scalar_replacement_opt(ir_graph *irg) {
634 scalars_t key, *value;
643 if (! get_opt_scalar_replacement())
646 rem = current_ir_graph;
647 current_ir_graph = irg;
649 /* Call algorithm that computes the out edges */
650 assure_irg_outs(irg);
652 /* Find possible scalar replacements */
653 if (find_possible_replacements(irg)) {
654 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %s\n", get_entity_name(get_irg_entity(irg))));
656 /* Insert in set the scalar replacements. */
657 irg_frame = get_irg_frame(irg);
659 modes = NEW_ARR_F(ir_mode *, 16);
660 set_ent = new_set(ent_cmp, 8);
661 sels = pset_new_ptr(8);
663 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
664 ir_node *succ = get_irn_out(irg_frame, i);
667 ir_entity *ent = get_Sel_entity(succ);
669 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
672 ent_type = get_entity_type(ent);
675 key.ent_owner = get_entity_owner(ent);
676 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
679 if (is_Array_type(ent_type)) {
680 DB((dbg, SET_LEVEL_1, " found array %s\n", get_entity_name(ent)));
681 } else if (is_Struct_type(ent_type)) {
682 DB((dbg, SET_LEVEL_1, " found struct %s\n", get_entity_name(ent)));
683 } else if (is_atomic_type(ent_type))
684 DB((dbg, SET_LEVEL_1, " found atomic value %s\n", get_entity_name(ent)));
686 panic("Neither an array nor a struct or atomic value found in scalar replace");
688 #endif /* DEBUG_libfirm */
690 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
694 DB((dbg, SET_LEVEL_1, " %u values will be needed\n", nvals));
696 /* If scalars were found. */
698 do_scalar_replacements(sels, nvals, modes);
700 foreach_set(set_ent, value) {
701 remove_class_member(value->ent_owner, value->ent);
705 * We changed the graph, but did NOT introduce new blocks
706 * neither changed control flow, cf-backedges should be still
709 set_irg_outs_inconsistent(irg);
710 set_irg_loopinfo_inconsistent(irg);
719 current_ir_graph = rem;
723 void firm_init_scalar_replace(void) {
724 FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");