2 * Copyright (C) 1995-2011 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 Lowering of builtins to compiler-lib calls
23 * @author Matthias Braun
27 #include "lower_builtins.h"
35 #include "iroptimize.h"
38 static pmap *entities;
39 static bool dont_lower[ir_bk_last+1];
41 static const char *get_builtin_name(ir_builtin_kind kind)
44 case ir_bk_ffs: return "ffs";
45 case ir_bk_clz: return "clz";
46 case ir_bk_ctz: return "ctz";
47 case ir_bk_popcount: return "popcount";
48 case ir_bk_parity: return "parity";
49 case ir_bk_bswap: return "bswap";
52 case ir_bk_debugbreak:
53 case ir_bk_return_address:
54 case ir_bk_frame_address:
57 case ir_bk_inner_trampoline:
63 static const char *get_gcc_machmode(ir_type *type)
65 assert(is_Primitive_type(type));
66 switch (get_type_size_bytes(type)) {
70 panic("couldn't determine gcc machmode for type %+F", type);
74 static void replace_with_call(ir_node *node)
76 ir_graph *irg = get_irn_irg(node);
77 ir_node *block = get_nodes_block(node);
78 ir_builtin_kind kind = get_Builtin_kind(node);
79 const char *name = get_builtin_name(kind);
80 ir_type *mtp = get_Builtin_type(node);
81 ir_type *arg1 = get_method_param_type(mtp, 0);
82 dbg_info *dbgi = get_irn_dbg_info(node);
83 ir_node *mem = get_Builtin_mem(node);
84 const char *gcc_machmode = get_gcc_machmode(arg1);
85 int n_params = get_Builtin_n_params(node);
86 ir_node **params = get_Builtin_param_arr(node);
87 ir_type *res_type = get_method_res_type(mtp, 0);
88 ir_mode *res_mode = get_type_mode(res_type);
96 union symconst_symbol sym;
99 snprintf(buf, sizeof(buf), "__%s%s2", name, gcc_machmode);
100 id = new_id_from_str(buf);
102 entity = (ir_entity*)pmap_get(entities, id);
103 if (entity == NULL) {
104 entity = create_compilerlib_entity(id, mtp);
105 pmap_insert(entities, id, entity);
108 sym.entity_p = entity;
109 symconst = new_r_SymConst(irg, mode_P, sym, symconst_addr_ent);
110 call = new_rd_Call(dbgi, block, mem, symconst, n_params, params, mtp);
111 call_mem = new_r_Proj(call, mode_M, pn_Call_M);
112 call_ress = new_r_Proj(call, mode_T, pn_Call_T_result);
113 call_res = new_r_Proj(call_ress, res_mode, 0);
115 turn_into_tuple(node, 2);
116 set_irn_n(node, pn_Builtin_M, call_mem);
117 set_irn_n(node, pn_Builtin_max+1, call_res);
120 static void lower_builtin(ir_node *node, void *env)
122 ir_builtin_kind kind;
124 if (!is_Builtin(node))
127 kind = get_Builtin_kind(node);
128 if (dont_lower[kind])
132 case ir_bk_prefetch: {
134 ir_node *mem = get_Builtin_mem(node);
135 turn_into_tuple(node, 1);
136 set_irn_n(node, pn_Builtin_M, mem);
145 /* replace with a call */
146 replace_with_call(node);
150 case ir_bk_debugbreak:
151 case ir_bk_return_address:
152 case ir_bk_frame_address:
155 case ir_bk_inner_trampoline:
156 /* can't do anything about these, backend will probably fail now */
157 panic("Can't lower Builtin node of kind %+F", node);
161 void lower_builtins(size_t n_exceptions, ir_builtin_kind *exceptions)
165 memset(dont_lower, 0, sizeof(dont_lower));
166 for (i = 0; i < n_exceptions; ++i) {
167 dont_lower[exceptions[i]] = true;
170 entities = pmap_create();
172 n_irgs = get_irp_n_irgs();
173 for (i = 0; i < n_irgs; ++i) {
174 ir_graph *irg = get_irp_irg(i);
175 irg_walk_graph(irg, NULL, lower_builtin, NULL);
178 pmap_destroy(entities);