Added option to prevent creation of misaligned loads/stores during CopyB lowering.
[libfirm] / ir / lower / lower_copyb.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Lower small CopyB nodes into a series of Load/Store nodes
23  * @author  Michael Beck, Matthias Braun, Manuel Mohr
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "adt/list.h"
29 #include "ircons.h"
30 #include "lowering.h"
31 #include "irprog_t.h"
32 #include "irgwalk.h"
33 #include "irnode_t.h"
34 #include "type_t.h"
35 #include "irtools.h"
36 #include "irgmod.h"
37 #include "error.h"
38 #include "be.h"
39
40 typedef struct entry entry_t;
41 struct entry {
42         struct list_head list;
43         ir_node *copyb;
44 };
45
46 /**
47  * Every CopyB is assigned a size category as follows:
48  *  - 'small'  iff                  size <= max_small_size,
49  *  - 'medium' iff max_small_size < size <  min_large_size,
50  *  - 'large'  iff                  size >= min_large_size.
51  *
52  * The idea is that each backend can apply different optimizations in each
53  * of the three categories.
54  *
55  * For small CopyBs, the x86 backend could, e.g., emit a single SSE
56  * instruction to copy 16 bytes.  Other backends might just go with a series
57  * of Load/Stores.  Therefore, x86 would like to keep the small CopyB nodes
58  * around whereas other backends would not.
59  * For medium-sized CopyBs, the x86 backend might generate a rep-prefixed mov
60  * instruction.  Hence, it also wants to keep the CopyBs in these cases.  Other
61  * backends might handle this differently.
62  * For large CopyBs, a call to memcpy is worth the call overhead, so large
63  * CopyBs should always be lowered to memcpy calls.
64  *
65  * The lowerer performs the following actions if the CopyB is
66  * - 'small':  Replace it with a series of Loads/Stores
67  * - 'medium': Nothing.
68  * - 'large':  Replace it with a call to memcpy.
69  *
70  * max_small_size and min_large_size allow for a flexible configuration.
71  * For example, one backend could specify max_small_size == 0 and
72  * min_large_size == 8192 to keep all CopyB nodes smaller than 8192 and get
73  * memcpy Calls for all others.  Here, the set of small CopyBs is empty.
74  * Another backend could specify max_small_size == 63 and min_large_size == 64
75  * to lower all small CopyBs to Loads/Stores and all big CopyBs to memcpy.
76  * Hence, the set of medium-sized CopyBs is empty and this backend never
77  * sees a CopyB node at all.
78  * If memcpy is not available, min_large_size can be set to UINT_MAX to prevent
79  * the creation of calls to memcpy.  Note that CopyBs whose size is UINT_MAX
80  * will still be lowered to memcpy calls because we check if the size is greater
81  * *or equal* to min_large_size.  However, this should never occur in practice.
82  */
83
84 static unsigned max_small_size; /**< The maximum size of a CopyB node
85                                      so that it is regarded as 'small'. */
86 static unsigned min_large_size; /**< The minimum size of a CopyB node
87                                      so that it is regarded as 'large'. */
88 static unsigned native_mode_bytes; /**< The size of the native mode in bytes. */
89 static int allow_misalignments; /**< Whether backend can handle misaligned
90                                      loads and stores. */
91
92 typedef struct walk_env {
93         struct obstack   obst;           /**< the obstack where data is allocated
94                                               on. */
95         struct list_head list;           /**< the list of copyb nodes. */
96 } walk_env_t;
97
98 static ir_mode *get_ir_mode(unsigned mode_bytes)
99 {
100         switch (mode_bytes) {
101         case 1:  return mode_Bu;
102         case 2:  return mode_Hu;
103         case 4:  return mode_Iu;
104         case 8:  return mode_Lu;
105         case 16: return mode_LLu;
106         default:
107                 panic("unexpected mode size requested in copyb lowering");
108         }
109 }
110
111 /**
112  * Turn a small CopyB node into a series of Load/Store nodes.
113  */
114 static void lower_small_copyb_node(ir_node *irn)
115 {
116         ir_graph *irg        = get_irn_irg(irn);
117         ir_node  *block      = get_nodes_block(irn);
118         ir_type  *tp         = get_CopyB_type(irn);
119         ir_node  *addr_src   = get_CopyB_src(irn);
120         ir_node  *addr_dst   = get_CopyB_dst(irn);
121         ir_node  *mem        = get_CopyB_mem(irn);
122         ir_mode  *addr_mode  = get_irn_mode(addr_src);
123         unsigned  mode_bytes = allow_misalignments ? native_mode_bytes : tp->align;
124         unsigned  size       = get_type_size_bytes(tp);
125         unsigned  offset     = 0;
126         ir_mode  *mode;
127
128         while (offset < size) {
129                 mode = get_ir_mode(mode_bytes);
130                 for (; offset + mode_bytes <= size; offset += mode_bytes) {
131                         /* construct offset */
132                         ir_node *addr_const;
133                         ir_node *add;
134                         ir_node *load;
135                         ir_node *load_res;
136                         ir_node *load_mem;
137                         ir_node *store;
138                         ir_node *store_mem;
139
140                         addr_const = new_r_Const_long(irg, mode_Iu, offset);
141                         add        = new_r_Add(block, addr_src, addr_const, addr_mode);
142
143                         load     = new_r_Load(block, mem, add, mode, cons_none);
144                         load_res = new_r_Proj(load, mode, pn_Load_res);
145                         load_mem = new_r_Proj(load, mode_M, pn_Load_M);
146
147                         addr_const = new_r_Const_long(irg, mode_Iu, offset);
148                         add        = new_r_Add(block, addr_dst, addr_const, addr_mode);
149
150                         store     = new_r_Store(block, load_mem, add, load_res, cons_none);
151                         store_mem = new_r_Proj(store, mode_M, pn_Store_M);
152
153                         mem = store_mem;
154                 }
155
156                 mode_bytes /= 2;
157         }
158
159         turn_into_tuple(irn, pn_CopyB_max+1);
160         set_Tuple_pred(irn, pn_CopyB_M,         mem);
161         set_Tuple_pred(irn, pn_CopyB_X_regular, new_r_Bad(irg, mode_X));
162         set_Tuple_pred(irn, pn_CopyB_X_except,  new_r_Bad(irg, mode_X));
163 }
164
165 static ir_type *get_memcpy_methodtype(void)
166 {
167         ir_type *tp          = new_type_method(3, 1);
168         ir_mode *size_t_mode = get_ir_mode(native_mode_bytes);
169
170         set_method_param_type(tp, 0, get_type_for_mode(mode_P));
171         set_method_param_type(tp, 1, get_type_for_mode(mode_P));
172         set_method_param_type(tp, 2, get_type_for_mode(size_t_mode));
173         set_method_res_type  (tp, 0, get_type_for_mode(mode_P));
174
175         return tp;
176 }
177
178 static ir_node *get_memcpy_symconst(ir_graph *irg)
179 {
180         ident     *id  = new_id_from_str("memcpy");
181         ir_type   *mt  = get_memcpy_methodtype();
182         ir_entity *ent = create_compilerlib_entity(id, mt);
183         symconst_symbol sym;
184
185         sym.entity_p = ent;
186         return new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
187 }
188
189 /**
190  * Turn a large CopyB node into a memcpy call.
191  */
192 static void lower_large_copyb_node(ir_node *irn)
193 {
194         ir_graph *irg      = get_irn_irg(irn);
195         ir_node  *block    = get_nodes_block(irn);
196         dbg_info *dbgi     = get_irn_dbg_info(irn);
197         ir_node  *mem      = get_CopyB_mem(irn);
198         ir_node  *addr_src = get_CopyB_src(irn);
199         ir_node  *addr_dst = get_CopyB_dst(irn);
200         ir_type  *copyb_tp = get_CopyB_type(irn);
201         unsigned  size     = get_type_size_bytes(copyb_tp);
202
203         ir_node  *symconst    = get_memcpy_symconst(irg);
204         ir_type  *call_tp     = get_memcpy_methodtype();
205         ir_mode  *mode_size_t = get_ir_mode(native_mode_bytes);
206         ir_node  *in[3];
207         ir_node  *call;
208         ir_node  *call_mem;
209
210         in[0]    = addr_dst;
211         in[1]    = addr_src;
212         in[2]    = new_r_Const_long(irg, mode_size_t, size);
213         call     = new_rd_Call(dbgi, block, mem, symconst, 3, in, call_tp);
214         call_mem = new_r_Proj(call, mode_M, pn_Call_M);
215
216         turn_into_tuple(irn, 1);
217         set_irn_n(irn, pn_CopyB_M, call_mem);
218 }
219
220 static void lower_copyb_node(ir_node *irn)
221 {
222         ir_type *tp   = get_CopyB_type(irn);
223         unsigned size = get_type_size_bytes(tp);
224
225         if (size <= max_small_size)
226                 lower_small_copyb_node(irn);
227         else if (size >= min_large_size)
228                 lower_large_copyb_node(irn);
229         else
230                 assert(!"CopyB of invalid size handed to lower_copyb_node");
231 }
232
233 /**
234  * Post-Walker: find CopyB nodes.
235  */
236 static void find_copyb_nodes(ir_node *irn, void *ctx)
237 {
238         walk_env_t *env = (walk_env_t*)ctx;
239         ir_type    *tp;
240         unsigned   size;
241         entry_t    *entry;
242         bool        medium_sized;
243
244         if (is_Proj(irn)) {
245                 ir_node *pred = get_Proj_pred(irn);
246
247                 if (is_CopyB(pred) && get_Proj_proj(irn) != pn_CopyB_M) {
248                         /* found an exception Proj: remove it from the list again */
249                         entry = (entry_t*)get_irn_link(pred);
250                         list_del(&entry->list);
251                 }
252                 return;
253         }
254
255         if (! is_CopyB(irn))
256                 return;
257
258         tp = get_CopyB_type(irn);
259         if (get_type_state(tp) != layout_fixed)
260                 return;
261
262         size         = get_type_size_bytes(tp);
263         medium_sized = max_small_size < size && size < min_large_size;
264         if (medium_sized)
265                 return; /* Nothing to do for medium-sized CopyBs. */
266
267         /* Okay, either small or large CopyB, so link it in and lower it later. */
268         entry = OALLOC(&env->obst, entry_t);
269         entry->copyb = irn;
270         INIT_LIST_HEAD(&entry->list);
271         set_irn_link(irn, entry);
272         list_add_tail(&entry->list, &env->list);
273 }
274
275 void lower_CopyB(ir_graph *irg, unsigned max_small_sz, unsigned min_large_sz,
276                  int allow_misaligns)
277 {
278         const backend_params *bparams = be_get_backend_param();
279         walk_env_t            env;
280         entry_t              *entry;
281
282         assert(max_small_sz < min_large_sz && "CopyB size ranges must not overlap");
283
284         max_small_size      = max_small_sz;
285         min_large_size      = min_large_sz;
286         native_mode_bytes   = bparams->machine_size / 8;
287         allow_misalignments = allow_misaligns;
288
289         obstack_init(&env.obst);
290         INIT_LIST_HEAD(&env.list);
291         irg_walk_graph(irg, NULL, find_copyb_nodes, &env);
292
293         list_for_each_entry(entry_t, entry, &env.list, list) {
294                 lower_copyb_node(entry->copyb);
295         }
296
297         obstack_free(&env.obst, NULL);
298 }