2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Lower small CopyB nodes into a series of Load/Store nodes
9 * @author Michael Beck, Matthias Braun, Manuel Mohr
25 typedef struct entry entry_t;
27 struct list_head list;
32 * Every CopyB is assigned a size category as follows:
33 * - 'small' iff size <= max_small_size,
34 * - 'medium' iff max_small_size < size < min_large_size,
35 * - 'large' iff size >= min_large_size.
37 * The idea is that each backend can apply different optimizations in each
38 * of the three categories.
40 * For small CopyBs, the x86 backend could, e.g., emit a single SSE
41 * instruction to copy 16 bytes. Other backends might just go with a series
42 * of Load/Stores. Therefore, x86 would like to keep the small CopyB nodes
43 * around whereas other backends would not.
44 * For medium-sized CopyBs, the x86 backend might generate a rep-prefixed mov
45 * instruction. Hence, it also wants to keep the CopyBs in these cases. Other
46 * backends might handle this differently.
47 * For large CopyBs, a call to memcpy is worth the call overhead, so large
48 * CopyBs should always be lowered to memcpy calls.
50 * The lowerer performs the following actions if the CopyB is
51 * - 'small': Replace it with a series of Loads/Stores
52 * - 'medium': Nothing.
53 * - 'large': Replace it with a call to memcpy.
55 * max_small_size and min_large_size allow for a flexible configuration.
56 * For example, one backend could specify max_small_size == 0 and
57 * min_large_size == 8192 to keep all CopyB nodes smaller than 8192 and get
58 * memcpy Calls for all others. Here, the set of small CopyBs is empty.
59 * Another backend could specify max_small_size == 63 and min_large_size == 64
60 * to lower all small CopyBs to Loads/Stores and all big CopyBs to memcpy.
61 * Hence, the set of medium-sized CopyBs is empty and this backend never
62 * sees a CopyB node at all.
63 * If memcpy is not available, min_large_size can be set to UINT_MAX to prevent
64 * the creation of calls to memcpy. Note that CopyBs whose size is UINT_MAX
65 * will still be lowered to memcpy calls because we check if the size is greater
66 * *or equal* to min_large_size. However, this should never occur in practice.
69 static unsigned max_small_size; /**< The maximum size of a CopyB node
70 so that it is regarded as 'small'. */
71 static unsigned min_large_size; /**< The minimum size of a CopyB node
72 so that it is regarded as 'large'. */
73 static unsigned native_mode_bytes; /**< The size of the native mode in bytes. */
74 static int allow_misalignments; /**< Whether backend can handle misaligned
77 typedef struct walk_env {
78 struct obstack obst; /**< the obstack where data is allocated
80 struct list_head list; /**< the list of copyb nodes. */
83 static ir_mode *get_ir_mode(unsigned mode_bytes)
86 case 1: return mode_Bu;
87 case 2: return mode_Hu;
88 case 4: return mode_Iu;
89 case 8: return mode_Lu;
90 case 16: return mode_LLu;
92 panic("unexpected mode size requested in copyb lowering");
97 * Turn a small CopyB node into a series of Load/Store nodes.
99 static void lower_small_copyb_node(ir_node *irn)
101 ir_graph *irg = get_irn_irg(irn);
102 ir_node *block = get_nodes_block(irn);
103 ir_type *tp = get_CopyB_type(irn);
104 ir_node *addr_src = get_CopyB_src(irn);
105 ir_node *addr_dst = get_CopyB_dst(irn);
106 ir_node *mem = get_CopyB_mem(irn);
107 ir_mode *addr_mode = get_irn_mode(addr_src);
108 unsigned mode_bytes = allow_misalignments ? native_mode_bytes : tp->align;
109 unsigned size = get_type_size_bytes(tp);
113 while (offset < size) {
114 mode = get_ir_mode(mode_bytes);
115 for (; offset + mode_bytes <= size; offset += mode_bytes) {
116 /* construct offset */
125 addr_const = new_r_Const_long(irg, mode_Iu, offset);
126 add = new_r_Add(block, addr_src, addr_const, addr_mode);
128 load = new_r_Load(block, mem, add, mode, cons_none);
129 load_res = new_r_Proj(load, mode, pn_Load_res);
130 load_mem = new_r_Proj(load, mode_M, pn_Load_M);
132 addr_const = new_r_Const_long(irg, mode_Iu, offset);
133 add = new_r_Add(block, addr_dst, addr_const, addr_mode);
135 store = new_r_Store(block, load_mem, add, load_res, cons_none);
136 store_mem = new_r_Proj(store, mode_M, pn_Store_M);
144 ir_node *const bad = new_r_Bad(irg, mode_X);
145 ir_node *const in[] = {
147 [pn_CopyB_X_regular] = bad,
148 [pn_CopyB_X_except] = bad,
150 turn_into_tuple(irn, ARRAY_SIZE(in), in);
153 static ir_type *get_memcpy_methodtype(void)
155 ir_type *tp = new_type_method(3, 1);
156 ir_mode *size_t_mode = get_ir_mode(native_mode_bytes);
158 set_method_param_type(tp, 0, get_type_for_mode(mode_P));
159 set_method_param_type(tp, 1, get_type_for_mode(mode_P));
160 set_method_param_type(tp, 2, get_type_for_mode(size_t_mode));
161 set_method_res_type (tp, 0, get_type_for_mode(mode_P));
166 static ir_node *get_memcpy_symconst(ir_graph *irg)
168 ident *id = new_id_from_str("memcpy");
169 ir_type *mt = get_memcpy_methodtype();
170 ir_entity *ent = create_compilerlib_entity(id, mt);
174 return new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
178 * Turn a large CopyB node into a memcpy call.
180 static void lower_large_copyb_node(ir_node *irn)
182 ir_graph *irg = get_irn_irg(irn);
183 ir_node *block = get_nodes_block(irn);
184 dbg_info *dbgi = get_irn_dbg_info(irn);
185 ir_node *mem = get_CopyB_mem(irn);
186 ir_node *addr_src = get_CopyB_src(irn);
187 ir_node *addr_dst = get_CopyB_dst(irn);
188 ir_type *copyb_tp = get_CopyB_type(irn);
189 unsigned size = get_type_size_bytes(copyb_tp);
191 ir_node *symconst = get_memcpy_symconst(irg);
192 ir_type *call_tp = get_memcpy_methodtype();
193 ir_mode *mode_size_t = get_ir_mode(native_mode_bytes);
200 in[2] = new_r_Const_long(irg, mode_size_t, size);
201 call = new_rd_Call(dbgi, block, mem, symconst, 3, in, call_tp);
202 call_mem = new_r_Proj(call, mode_M, pn_Call_M);
204 ir_node *const tuple_in[] = { call_mem };
205 turn_into_tuple(irn, ARRAY_SIZE(tuple_in), tuple_in);
208 static void lower_copyb_node(ir_node *irn)
210 ir_type *tp = get_CopyB_type(irn);
211 unsigned size = get_type_size_bytes(tp);
213 if (size <= max_small_size)
214 lower_small_copyb_node(irn);
215 else if (size >= min_large_size)
216 lower_large_copyb_node(irn);
218 assert(!"CopyB of invalid size handed to lower_copyb_node");
222 * Post-Walker: find CopyB nodes.
224 static void find_copyb_nodes(ir_node *irn, void *ctx)
226 walk_env_t *env = (walk_env_t*)ctx;
233 ir_node *pred = get_Proj_pred(irn);
235 if (is_CopyB(pred) && get_Proj_proj(irn) != pn_CopyB_M) {
236 /* found an exception Proj: remove it from the list again */
237 entry = (entry_t*)get_irn_link(pred);
238 list_del(&entry->list);
246 tp = get_CopyB_type(irn);
247 if (get_type_state(tp) != layout_fixed)
250 size = get_type_size_bytes(tp);
251 medium_sized = max_small_size < size && size < min_large_size;
253 return; /* Nothing to do for medium-sized CopyBs. */
255 /* Okay, either small or large CopyB, so link it in and lower it later. */
256 entry = OALLOC(&env->obst, entry_t);
258 INIT_LIST_HEAD(&entry->list);
259 set_irn_link(irn, entry);
260 list_add_tail(&entry->list, &env->list);
263 void lower_CopyB(ir_graph *irg, unsigned max_small_sz, unsigned min_large_sz,
266 const backend_params *bparams = be_get_backend_param();
269 assert(max_small_sz < min_large_sz && "CopyB size ranges must not overlap");
271 max_small_size = max_small_sz;
272 min_large_size = min_large_sz;
273 native_mode_bytes = bparams->machine_size / 8;
274 allow_misalignments = allow_misaligns;
276 obstack_init(&env.obst);
277 INIT_LIST_HEAD(&env.list);
278 irg_walk_graph(irg, NULL, find_copyb_nodes, &env);
280 list_for_each_entry(entry_t, entry, &env.list, list) {
281 lower_copyb_node(entry->copyb);
284 obstack_free(&env.obst, NULL);