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