cleanup: Remove duplicate MIN/MAX macros.
[libfirm] / ir / lower / lower_copyb.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Lower small CopyB nodes into a series of Load/Store nodes
9  * @author  Michael Beck, Matthias Braun, Manuel Mohr
10  */
11 #include "config.h"
12
13 #include "adt/list.h"
14 #include "ircons.h"
15 #include "lowering.h"
16 #include "irprog_t.h"
17 #include "irgwalk.h"
18 #include "irnode_t.h"
19 #include "type_t.h"
20 #include "irgmod.h"
21 #include "error.h"
22 #include "be.h"
23 #include "util.h"
24
25 typedef struct entry entry_t;
26 struct entry {
27         struct list_head list;
28         ir_node *copyb;
29 };
30
31 /**
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.
36  *
37  * The idea is that each backend can apply different optimizations in each
38  * of the three categories.
39  *
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.
49  *
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.
54  *
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.
67  */
68
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
75                                      loads and stores. */
76
77 typedef struct walk_env {
78         struct obstack   obst;           /**< the obstack where data is allocated
79                                               on. */
80         struct list_head list;           /**< the list of copyb nodes. */
81 } walk_env_t;
82
83 static ir_mode *get_ir_mode(unsigned mode_bytes)
84 {
85         switch (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;
91         default:
92                 panic("unexpected mode size requested in copyb lowering");
93         }
94 }
95
96 /**
97  * Turn a small CopyB node into a series of Load/Store nodes.
98  */
99 static void lower_small_copyb_node(ir_node *irn)
100 {
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);
110         unsigned  offset     = 0;
111         ir_mode  *mode;
112
113         while (offset < size) {
114                 mode = get_ir_mode(mode_bytes);
115                 for (; offset + mode_bytes <= size; offset += mode_bytes) {
116                         /* construct offset */
117                         ir_node *addr_const;
118                         ir_node *add;
119                         ir_node *load;
120                         ir_node *load_res;
121                         ir_node *load_mem;
122                         ir_node *store;
123                         ir_node *store_mem;
124
125                         addr_const = new_r_Const_long(irg, mode_Iu, offset);
126                         add        = new_r_Add(block, addr_src, addr_const, addr_mode);
127
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);
131
132                         addr_const = new_r_Const_long(irg, mode_Iu, offset);
133                         add        = new_r_Add(block, addr_dst, addr_const, addr_mode);
134
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);
137
138                         mem = store_mem;
139                 }
140
141                 mode_bytes /= 2;
142         }
143
144         ir_node *const bad = new_r_Bad(irg, mode_X);
145         ir_node *const in[] = {
146                 [pn_CopyB_M]         = mem,
147                 [pn_CopyB_X_regular] = bad,
148                 [pn_CopyB_X_except]  = bad,
149         };
150         turn_into_tuple(irn, ARRAY_SIZE(in), in);
151 }
152
153 static ir_type *get_memcpy_methodtype(void)
154 {
155         ir_type *tp          = new_type_method(3, 1);
156         ir_mode *size_t_mode = get_ir_mode(native_mode_bytes);
157
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));
162
163         return tp;
164 }
165
166 static ir_node *get_memcpy_symconst(ir_graph *irg)
167 {
168         ident     *id  = new_id_from_str("memcpy");
169         ir_type   *mt  = get_memcpy_methodtype();
170         ir_entity *ent = create_compilerlib_entity(id, mt);
171         symconst_symbol sym;
172
173         sym.entity_p = ent;
174         return new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
175 }
176
177 /**
178  * Turn a large CopyB node into a memcpy call.
179  */
180 static void lower_large_copyb_node(ir_node *irn)
181 {
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);
190
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);
194         ir_node  *in[3];
195         ir_node  *call;
196         ir_node  *call_mem;
197
198         in[0]    = addr_dst;
199         in[1]    = addr_src;
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);
203
204         ir_node *const tuple_in[] = { call_mem };
205         turn_into_tuple(irn, ARRAY_SIZE(tuple_in), tuple_in);
206 }
207
208 static void lower_copyb_node(ir_node *irn)
209 {
210         ir_type *tp   = get_CopyB_type(irn);
211         unsigned size = get_type_size_bytes(tp);
212
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);
217         else
218                 assert(!"CopyB of invalid size handed to lower_copyb_node");
219 }
220
221 /**
222  * Post-Walker: find CopyB nodes.
223  */
224 static void find_copyb_nodes(ir_node *irn, void *ctx)
225 {
226         walk_env_t *env = (walk_env_t*)ctx;
227         ir_type    *tp;
228         unsigned   size;
229         entry_t    *entry;
230         bool        medium_sized;
231
232         if (is_Proj(irn)) {
233                 ir_node *pred = get_Proj_pred(irn);
234
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);
239                 }
240                 return;
241         }
242
243         if (! is_CopyB(irn))
244                 return;
245
246         tp = get_CopyB_type(irn);
247         if (get_type_state(tp) != layout_fixed)
248                 return;
249
250         size         = get_type_size_bytes(tp);
251         medium_sized = max_small_size < size && size < min_large_size;
252         if (medium_sized)
253                 return; /* Nothing to do for medium-sized CopyBs. */
254
255         /* Okay, either small or large CopyB, so link it in and lower it later. */
256         entry = OALLOC(&env->obst, entry_t);
257         entry->copyb = irn;
258         INIT_LIST_HEAD(&entry->list);
259         set_irn_link(irn, entry);
260         list_add_tail(&entry->list, &env->list);
261 }
262
263 void lower_CopyB(ir_graph *irg, unsigned max_small_sz, unsigned min_large_sz,
264                  int allow_misaligns)
265 {
266         const backend_params *bparams = be_get_backend_param();
267         walk_env_t            env;
268
269         assert(max_small_sz < min_large_sz && "CopyB size ranges must not overlap");
270
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;
275
276         obstack_init(&env.obst);
277         INIT_LIST_HEAD(&env.list);
278         irg_walk_graph(irg, NULL, find_copyb_nodes, &env);
279
280         list_for_each_entry(entry_t, entry, &env.list, list) {
281                 lower_copyb_node(entry->copyb);
282         }
283
284         obstack_free(&env.obst, NULL);
285 }