irgmod.c 5.56 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
2
/*
 * Project:     libFIRM
Michael Beck's avatar
Michael Beck committed
3
 * File name:   ir/ir/irgmod.c
Götz Lindenmaier's avatar
Götz Lindenmaier committed
4
5
6
7
8
9
10
11
 * Purpose:     Support for ir graph modification.
 * Author:      Martin Trapp, Christian Schaefer
 * Modified by: Goetz Lindenmaier
 * Created:
 * CVS-ID:      $Id$
 * Copyright:   (c) 1998-2003 Universitt Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 */
Boris Boesler's avatar
Boris Boesler committed
12

Boris Boesler's avatar
added    
Boris Boesler committed
13
#ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
14
# include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
15
16
#endif

Michael Beck's avatar
Michael Beck committed
17
18
19
20
21
22
23
24
25
26
27
#include "irvrfy.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irgmod.h"
#include "array.h"
#include "ircons.h"
#include "irhooks.h"
#include "iredges_t.h"
#include "irtools.h"
Christian Schäfer's avatar
Christian Schäfer committed
28

Götz Lindenmaier's avatar
Götz Lindenmaier committed
29
30
31
32
/* Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
   from several inputs.
   This is useful if a node returning a tuple is removed, but the Projs
   extracting values from the tuple are not available. */
Christian Schäfer's avatar
Christian Schäfer committed
33
34
35
36
37
void
turn_into_tuple (ir_node *node, int arity)
{
  assert(node);
  set_irn_op(node, op_Tuple);
38
  if (get_irn_arity(node) == arity) {
Christian Schäfer's avatar
Christian Schäfer committed
39
40
    /* keep old array */
  } else {
Götz Lindenmaier's avatar
Götz Lindenmaier committed
41
    /* Allocate new array, don't free old in_array, it's on the obstack. */
42
    ir_node *block = get_nodes_block(node);
Sebastian Hack's avatar
Sebastian Hack committed
43
    edges_node_deleted(node, current_ir_graph);
44
45
46
    node->in = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity+1);
    /* clear the new in array, else edge_notify tries to delete garbage */
    memset(node->in, 0, (arity+1) * sizeof(node->in[0]));
47
    set_nodes_block(node, block);
Christian Schäfer's avatar
Christian Schäfer committed
48
49
50
51
52
53
  }
}

/* Insert irnode `new' in place of irnode `old'
   Since `new' may be bigger than `old' replace `old'
   by an op_Id which is smaller than everything */
54
void
Götz Lindenmaier's avatar
Götz Lindenmaier committed
55
exchange (ir_node *old, ir_node *nw)
Christian Schäfer's avatar
Christian Schäfer committed
56
{
57
  assert(old != nw && "Exchanging node with itself is not allowed");
58
59
  hook_replace(old, nw);

Götz Lindenmaier's avatar
Götz Lindenmaier committed
60
61
62
63
64
  /*
   * If new outs are on, we can skip the id node creation and reroute
   * the edges from the old node to the new directly.
   */
  if (edges_activated(current_ir_graph)) {
65
66
67
    /* copy all dependencies from old to new */
    add_irn_deps(nw, old);

Götz Lindenmaier's avatar
Götz Lindenmaier committed
68
    edges_reroute(old, nw, current_ir_graph);
69
    edges_reroute_kind(old, nw, EDGE_KIND_DEP, current_ir_graph);
70
71
    edges_node_deleted(old, current_ir_graph);
    old->op = op_Bad;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
72
  }
73
74
  else {
    /* Else, do it the old-fashioned way. */
75

Götz Lindenmaier's avatar
Götz Lindenmaier committed
76
77
    ir_graph *irg = get_irn_irg (old);
    ir_node *block;
78

Götz Lindenmaier's avatar
Götz Lindenmaier committed
79
80
81
    assert(old != nw);
    assert (irg);
    assert(get_irn_op(old)->opar != oparity_dynamic);
82

Götz Lindenmaier's avatar
Götz Lindenmaier committed
83
    hook_turn_into_id(old);
84

Götz Lindenmaier's avatar
Götz Lindenmaier committed
85
86
87
    block = old->in[0];
    if (!block) {
      block = is_Block(nw) ? nw : get_nodes_block(nw);
88

Götz Lindenmaier's avatar
Götz Lindenmaier committed
89
      if (!block) {
90
91
92
        DDMN(old);
        DDMN(nw);
        assert(0 && "cannot find legal block for id");
Götz Lindenmaier's avatar
Götz Lindenmaier committed
93
94
      }
    }
Christian Schäfer's avatar
Christian Schäfer committed
95

Götz Lindenmaier's avatar
Götz Lindenmaier committed
96
97
98
99
100
    old->op = op_Id;
    old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
    old->in[0] = block;
    old->in[1] = nw;
  }
Christian Schäfer's avatar
Christian Schäfer committed
101
}
102

103
104
105
/*--------------------------------------------------------------------*/
/*  Functionality for collect_phis                                    */
/*--------------------------------------------------------------------*/
106

Michael Beck's avatar
Michael Beck committed
107
108
109
110
111
/**
 * Walker: links all Phi nodes to their Blocks and
 *         all Proj nodes to there predecessors
 */
static void collect(ir_node *n, void *env) {
112
  ir_node *pred;
Michael Beck's avatar
Michael Beck committed
113
114

  if (is_Phi(n)) {
115
116
    set_irn_link(n, get_irn_link(get_nodes_block(n)));
    set_irn_link(get_nodes_block(n), n);
117
  }
Michael Beck's avatar
Michael Beck committed
118
  else if (is_Proj(n)) {
119
    pred = n;
Michael Beck's avatar
Michael Beck committed
120
    do {
121
      pred = get_Proj_pred(pred);
Michael Beck's avatar
Michael Beck committed
122
123
    } while (is_Proj(pred));

124
125
126
127
128
129
    set_irn_link(n, get_irn_link(pred));
    set_irn_link(pred, n);
  }
}

void collect_phiprojs(ir_graph *irg) {
Michael Beck's avatar
Michael Beck committed
130
  irg_walk_graph(irg, firm_clear_link, collect, NULL);
131
132
133
}


134
135
136
/*--------------------------------------------------------------------*/
/*  Functionality for part_block                                      */
/*--------------------------------------------------------------------*/
137

Michael Beck's avatar
Michael Beck committed
138
139
140
141
/**
 * Moves node and all predecessors of node from from_bl to to_bl.
 * Does not move predecessors of Phi nodes (or block nodes).
 */
142
static void move (ir_node *node, ir_node *from_bl, ir_node *to_bl) {
143
  int i, arity;
144
145
146
  ir_node *proj, *pred;

  /* move this node */
147
  set_nodes_block(node, to_bl);
148
149

  /* move its projs */
150
  if (get_irn_mode(node) == mode_T) {
151
152
    proj = get_irn_link(node);
    while (proj) {
153
      if (get_nodes_block(proj) == from_bl)
Michael Beck's avatar
Michael Beck committed
154
        set_nodes_block(proj, to_bl);
155
156
157
158
159
      proj = get_irn_link(proj);
    }
  }

  /* recursion ... */
160
  if (get_irn_op(node) == op_Phi) return;
161

162
163
  arity = get_irn_arity(node);
  for (i = 0; i < arity; i++) {
164
    pred = get_irn_n(node, i);
165
    if (get_nodes_block(pred) == from_bl)
166
167
168
169
170
171
172
173
174
      move(pred, from_bl, to_bl);
  }
}

void part_block(ir_node *node) {
  ir_node *new_block;
  ir_node *old_block;
  ir_node *phi;

175
  /* Turn off optimizations so that blocks are not merged again. */
176
  int rem_opt = get_opt_optimize();
177
178
  set_optimize(0);

179
  /* Transform the control flow */
180
  old_block = get_nodes_block(node);
181
  new_block = new_Block(get_Block_n_cfgpreds(old_block),
Florian Liekweg's avatar
Florian Liekweg committed
182
            get_Block_cfgpred_arr(old_block));
183
184
  set_irg_current_block(current_ir_graph, new_block);
  {
Michael Beck's avatar
Michael Beck committed
185
186
    ir_node *jmp = new_Jmp();
    set_irn_in(old_block, 1, &jmp);
187
    irn_vrfy_irg(old_block, current_ir_graph);
188
189
190
191
192
193
194
195
196
197
  }

  /* move node and its predecessors to new_block */
  move(node, old_block, new_block);

  /* move Phi nodes to new_block */
  phi = get_irn_link(old_block);
  set_irn_link(new_block, phi);
  set_irn_link(old_block, NULL);
  while (phi) {
198
    if(get_nodes_block(phi) == old_block);   /* @@@ inlinening chokes on phis that don't
Florian Liekweg's avatar
Florian Liekweg committed
199
200
                           obey this condition.  How do they get into
                           the list??? Example: InterfaceIII */
201
      set_nodes_block(phi, new_block);
202
203
    phi = get_irn_link(phi);
  }
204
205

  set_optimize(rem_opt);
206
}