+ set_r_cur_block(res, start_block);
+ set_r_store(res, initial_mem);
+
+ /*-- Make a block to start with --*/
+ first_block = new_r_Block(res, 1, &projX);
+ set_r_cur_block(res, first_block);
+
+ res->method_execution_frequency = -1.0;
+
+ return res;
+}
+
+ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
+{
+ ir_graph *res = new_r_ir_graph(ent, n_loc);
+ add_irp_irg(res); /* remember this graph global. */
+ return res;
+}
+
+ir_graph *new_const_code_irg(void)
+{
+ ir_graph *res = alloc_graph();
+ ir_node *body_block;
+ ir_node *end;
+ ir_node *end_block;
+ ir_node *no_mem;
+ ir_node *projX;
+ ir_node *start_block;
+ ir_node *start;
+
+ /* inform statistics here, as blocks will be already build on this graph */
+ hook_new_graph(res, NULL);
+
+ res->n_loc = 1; /* Only the memory. */
+ res->irg_pinned_state = op_pin_state_pinned;
+ res->fp_model = fp_model_precise;
+
+ add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
+
+ /* the Anchor node must be created first */
+ res->anchor = new_r_Anchor(res);
+
+ /* -- The end block -- */
+ end_block = new_r_Block_noopt(res, 0, NULL);
+ set_irg_end_block(res, end_block);
+ end = new_r_End(res, 0, NULL);
+ set_irg_end(res, end);
+
+ /* -- The start block -- */
+ start_block = new_r_Block_noopt(res, 0, NULL);
+ set_irg_start_block(res, start_block);
+ no_mem = new_r_NoMem(res);
+ set_irg_no_mem(res, no_mem);
+ start = new_r_Start(res);
+ set_irg_start(res, start);
+
+ /* Proj results of start node */
+ set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
+ projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
+
+ body_block = new_r_Block(res, 1, &projX);
+
+ set_r_cur_block(res, body_block);
+
+ /* Set the visited flag high enough that the blocks will never be visited. */
+ set_irn_visited(body_block, -1);
+ set_Block_block_visited(body_block, -1);
+ set_Block_block_visited(start_block, -1);
+ set_irn_visited(start_block, -1);
+
+ return res;
+}
+
+/**
+ * Pre-Walker: Copies blocks and nodes from the original method graph
+ * to the copied graph.
+ *
+ * @param n A node from the original method graph.
+ * @param env The copied graph.
+ */
+static void copy_all_nodes(ir_node *node, void *env)
+{
+ ir_graph *irg = (ir_graph*)env;
+ ir_node *new_node = irn_copy_into_irg(node, irg);
+
+ set_irn_link(node, new_node);
+
+ /* fix access to entities on the stack frame */
+ if (is_Sel(new_node)) {
+ ir_entity *ent = get_Sel_entity(new_node);
+ ir_type *tp = get_entity_owner(ent);
+
+ if (is_frame_type(tp)) {
+ /* replace by the copied entity */
+ ent = (ir_entity*)get_entity_link(ent);
+
+ assert(is_entity(ent));
+ assert(get_entity_owner(ent) == get_irg_frame_type(irg));
+ set_Sel_entity(new_node, ent);
+ }
+ }
+}
+
+/**
+ * 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 rewire(ir_node *irn, void *env)
+{
+ (void) env;
+ irn_rewire_inputs(irn);
+}
+
+static ir_node *get_new_node(const ir_node *old_node)
+{
+ return (ir_node*) get_irn_link(old_node);
+}
+
+ir_graph *create_irg_copy(ir_graph *irg)
+{
+ ir_graph *res;
+
+ res = alloc_graph();
+
+ res->irg_pinned_state = irg->irg_pinned_state;
+ res->fp_model = irg->fp_model;
+
+ /* clone the frame type here for safety */
+ irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
+ res->frame_type = clone_frame_type(irg->frame_type);
+
+ ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
+
+ /* copy all nodes from the graph irg to the new graph res */
+ irg_walk_anchors(irg, copy_all_nodes, rewire, res);
+
+ /* copy the Anchor node */
+ res->anchor = get_new_node(irg->anchor);
+
+ /* -- The end block -- */
+ set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
+ set_irg_end (res, get_new_node(get_irg_end(irg)));
+
+ /* -- The start block -- */
+ set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
+ set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
+ set_irg_start (res, get_new_node(get_irg_start(irg)));
+
+ /* Proj results of start node */
+ set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
+
+ ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
+ irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
+
+ return res;
+}
+
+void free_ir_graph(ir_graph *irg)
+{
+ assert(is_ir_graph(irg));
+
+ remove_irp_irg(irg);
+ confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
+
+ hook_free_graph(irg);
+ free_irg_outs(irg);
+ del_identities(irg);
+ if (irg->ent) {
+ set_entity_irg(irg->ent, NULL); /* not set in const code irg */
+ }
+
+ free_End(get_irg_end(irg));
+ obstack_free(&irg->obst, NULL);
+ if (irg->loc_descriptions)
+ free(irg->loc_descriptions);
+ irg->kind = k_BAD;
+ free_graph(irg);
+}
+
+int (is_ir_graph)(const void *thing)
+{
+ return is_ir_graph_(thing);
+}
+
+#ifdef DEBUG_libfirm
+long get_irg_graph_nr(const ir_graph *irg)
+{
+ return irg->graph_nr;
+}