+ q_set *hmap = (q_set*)env;
+ ir_node *call_ptr;
+ ir_entity *callee;
+
+ /* We collect just "Call" nodes */
+ if (is_Call(call)) {
+ call_ptr = get_Call_ptr(call);
+
+ if (! is_Global(call_ptr))
+ return;
+
+ callee = get_Global_entity(call_ptr);
+
+ /* we don't know which function gets finally bound to a weak symbol */
+ if (get_entity_linkage(callee) & IR_LINKAGE_WEAK)
+ return;
+
+ /* we can only clone calls to existing entities */
+ if (get_entity_irg(callee) == NULL)
+ return;
+
+ process_call(call, callee, hmap);
+ }
+}
+
+/**
+ * Make a name for a clone. The clone name is
+ * the name of the original method suffixed with "_cl_pos_nr".
+ * pos is the pos from our quadruplet and nr is a counter.
+ *
+ * @param id The ident of the cloned function.
+ * @param pos The "pos" from our quadruplet.
+ * @param nr A counter for the clones.
+ */
+static ident *get_clone_ident(ident *id, int pos, unsigned nr)
+{
+ char clone_postfix[32];
+
+ snprintf(clone_postfix, sizeof(clone_postfix), "_cl_%d_%u", pos, nr);
+
+ return id_mangle(id, new_id_from_str(clone_postfix));
+}
+
+/**
+ * Pre-Walker: Copies blocks and nodes from the original method graph
+ * to the cloned graph. Fixes the argument projection numbers for
+ * all arguments behind the removed one.
+ *
+ * @param irn A node from the original method graph.
+ * @param env The clone graph.
+ */
+static void copy_nodes(ir_node *irn, void *env)
+{
+ ir_graph *clone_irg = (ir_graph*)env;
+ ir_node *arg = (ir_node*)get_irg_link(clone_irg);
+ ir_node *irg_args = get_Proj_pred(arg);
+ ir_node *irn_copy;
+ long proj_nr;
+
+ /* Copy all nodes except the arg. */
+ if (irn != arg)
+ copy_irn_to_irg(irn, clone_irg);
+
+ irn_copy = (ir_node*)get_irn_link(irn);
+
+ /* Fix argument numbers */
+ if (is_Proj(irn) && get_Proj_pred(irn) == irg_args) {
+ proj_nr = get_Proj_proj(irn);
+ if (get_Proj_proj(arg) < proj_nr)
+ set_Proj_proj(irn_copy, proj_nr - 1);
+ }
+}
+
+/**
+ * Post-walker: Set the predecessors of the copied nodes.
+ * The copied nodes are set as link of their original nodes. The links of
+ * "irn" predecessors are the predecessors of copied node.
+ */
+static void set_preds(ir_node *irn, void *env)
+{
+ ir_graph *clone_irg = (ir_graph*)env;
+ ir_node *arg = (ir_node*)get_irg_link(clone_irg);
+ int i;
+ ir_node *irn_copy;
+ ir_node *pred;
+
+ /* Arg is the method argument, that we have replaced by a constant.*/
+ if (arg == irn)
+ return;
+
+ irn_copy = (ir_node*)get_irn_link(irn);
+
+ if (is_Block(irn)) {
+ for (i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
+ pred = get_Block_cfgpred(irn, i);
+ /* "End" block must be handled extra, because it is not matured.*/
+ if (get_irg_end_block(current_ir_graph) == irn)
+ add_immBlock_pred(get_irg_end_block(clone_irg), (ir_node*)get_irn_link(pred));
+ else
+ set_Block_cfgpred(irn_copy, i, (ir_node*)get_irn_link(pred));
+ }
+ } else {
+ /* First we set the block our copy if it is not a block.*/
+ set_nodes_block(irn_copy, (ir_node*)get_irn_link(get_nodes_block(irn)));
+ if (is_End(irn)) {
+ /* Handle the keep-alives. This must be done separately, because
+ the End node was NOT copied */
+ for (i = 0; i < get_End_n_keepalives(irn); ++i)
+ add_End_keepalive(irn_copy, (ir_node*)get_irn_link(get_End_keepalive(irn, i)));
+ } else {
+ for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
+ pred = get_irn_n(irn, i);
+ set_irn_n(irn_copy, i, (ir_node*)get_irn_link(pred));
+ }
+ }
+ }
+}
+
+/**
+ * Get the method argument at the position "pos".
+ *
+ * @param irg irg that must be cloned.
+ * @param pos The position of the argument.
+ */
+static ir_node *get_irg_arg(ir_graph *irg, int pos)
+{
+ ir_node *irg_args = get_irg_args(irg), *arg = NULL;
+ int i;
+
+ /* Call algorithm that computes the out edges */
+ assure_irg_outs(irg);
+
+ /* Search the argument with the number pos.*/
+ for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
+ ir_node *proj = get_irn_out(irg_args, i);
+ if (pos == get_Proj_proj(proj)) {
+ if (arg) {
+ /*
+ * More than one arg node found:
+ * We rely on the fact the only one arg exists, so do
+ * a cheap CSE in this case.
+ */
+ set_irn_out(irg_args, i, arg, 0);
+ exchange(proj, arg);
+ } else
+ arg = proj;
+ }
+ }
+ assert(arg && "Argument not found");
+ return arg;
+}
+
+/**
+ * Create a new graph for the clone of the method,
+ * that we want to clone.
+ *
+ * @param ent The entity of the method that must be cloned.
+ * @param q Our quadruplet.
+ */
+static void create_clone_proc_irg(ir_entity *ent, quadruple_t *q)
+{
+ ir_graph *method_irg, *clone_irg;
+ ir_node *arg, *const_arg;
+
+ method_irg = get_entity_irg(ent);
+
+ /* We create the skeleton of the clone irg.*/
+ clone_irg = new_ir_graph(ent, 0);
+
+ arg = get_irg_arg(get_entity_irg(q->ent), q->pos);
+ /* we will replace the argument in position "q->pos" by this constant. */
+ const_arg = new_r_Const(clone_irg, q->tv);
+
+ /* args copy in the cloned graph will be the const. */
+ set_irn_link(arg, const_arg);
+
+ /* Store the arg that will be replaced here, so we can easily detect it. */
+ set_irg_link(clone_irg, arg);
+
+ /* We copy the blocks and nodes, that must be in
+ the clone graph and set their predecessors. */
+ irg_walk_graph(method_irg, copy_nodes, set_preds, clone_irg);
+
+ /* The "cloned" graph must be matured. */
+ mature_immBlock(get_irg_end_block(clone_irg));
+ irg_finalize_cons(clone_irg);
+}
+
+/**
+ * The function create a new entity type
+ * for our clone and set it to clone entity.
+ *
+ * @param q Contains information for the method to clone.
+ * @param ent The entity of the clone.
+ * @param nr A pointer to the counter of clones.
+ **/
+static void change_entity_type(quadruple_t *q, ir_entity *ent)
+{
+ ir_type *mtp, *new_mtp, *tp;
+ int i, j, n_params, n_ress;
+
+ mtp = get_entity_type(q->ent);
+ n_params = get_method_n_params(mtp);
+ n_ress = get_method_n_ress(mtp);
+
+ /* Create the new type for our clone. It must have one parameter
+ less then the original.*/
+ new_mtp = new_type_method(n_params - 1, n_ress);
+
+ /* We must set the type of the methods parameters.*/
+ for (i = j = 0; i < n_params; ++i) {
+ if (i == q->pos)
+ /* This is the position of the argument, that we have
+ replaced. */
+ continue;
+
+ tp = get_method_param_type(mtp, i);
+ set_method_param_type(new_mtp, j++, tp);
+ }
+ /* Copy the methods result types. */
+ for (i = 0; i < n_ress; ++i) {
+ tp = get_method_res_type(mtp, i);
+ set_method_res_type(new_mtp, i, tp);
+ }
+ set_entity_type(ent, new_mtp);
+}
+
+/**
+ * Make a clone of a method.
+ *
+ * @param q Contains information for the method to clone.
+ */
+static ir_entity *clone_method(quadruple_t *q)
+{
+ ir_entity *new_entity;
+ ident *clone_ident;
+ symconst_symbol sym;
+ /* A counter for the clones.*/
+ static unsigned nr = 0;