irgmod.c 5.36 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);
43
44
45
46
    edges_invalidate(node, current_ir_graph);
    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
58
  hook_replace(old, nw);

Götz Lindenmaier's avatar
Götz Lindenmaier committed
59
60
61
62
63
64
65
  /*
   * 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)) {
    edges_reroute(old, nw, current_ir_graph);
  }
66
67
  else {
    /* Else, do it the old-fashioned way. */
68

Götz Lindenmaier's avatar
Götz Lindenmaier committed
69
70
    ir_graph *irg = get_irn_irg (old);
    ir_node *block;
71

Götz Lindenmaier's avatar
Götz Lindenmaier committed
72
73
74
    assert(old != nw);
    assert (irg);
    assert(get_irn_op(old)->opar != oparity_dynamic);
75

Götz Lindenmaier's avatar
Götz Lindenmaier committed
76
    hook_turn_into_id(old);
77

Götz Lindenmaier's avatar
Götz Lindenmaier committed
78
79
80
    block = old->in[0];
    if (!block) {
      block = is_Block(nw) ? nw : get_nodes_block(nw);
81

Götz Lindenmaier's avatar
Götz Lindenmaier committed
82
      if (!block) {
83
84
85
        DDMN(old);
        DDMN(nw);
        assert(0 && "cannot find legal block for id");
Götz Lindenmaier's avatar
Götz Lindenmaier committed
86
87
      }
    }
Christian Schäfer's avatar
Christian Schäfer committed
88

Götz Lindenmaier's avatar
Götz Lindenmaier committed
89
90
91
92
93
    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
94
}
95

96
97
98
/*--------------------------------------------------------------------*/
/*  Functionality for collect_phis                                    */
/*--------------------------------------------------------------------*/
99

100
static void
101
102
collect (ir_node *n, void *env) {
  ir_node *pred;
103
  if (get_irn_op(n) == op_Phi) {
104
105
    set_irn_link(n, get_irn_link(get_nodes_block(n)));
    set_irn_link(get_nodes_block(n), n);
106
  }
107
  if (get_irn_op(n) == op_Proj) {
108
    pred = n;
109
    while (get_irn_op(pred) == op_Proj)
110
111
112
113
114
115
116
117
118
119
120
121
122
      pred = get_Proj_pred(pred);
    set_irn_link(n, get_irn_link(pred));
    set_irn_link(pred, n);
  }
}

void collect_phiprojs(ir_graph *irg) {
  ir_graph *rem;

  /* Remember external state of current_ir_graph. */
  rem = current_ir_graph;
  current_ir_graph = irg;

Michael Beck's avatar
Michael Beck committed
123
  irg_walk(get_irg_end(current_ir_graph), firm_clear_link, collect, NULL);
124
125
126
127
128

  current_ir_graph = rem;
}


129
130
131
/*--------------------------------------------------------------------*/
/*  Functionality for part_block                                      */
/*--------------------------------------------------------------------*/
132

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

  /* move this node */
142
  set_nodes_block(node, to_bl);
143
144

  /* move its projs */
145
  if (get_irn_mode(node) == mode_T) {
146
147
    proj = get_irn_link(node);
    while (proj) {
148
149
      if (get_nodes_block(proj) == from_bl)
	set_nodes_block(proj, to_bl);
150
151
152
153
154
      proj = get_irn_link(proj);
    }
  }

  /* recursion ... */
155
  if (get_irn_op(node) == op_Phi) return;
156

157
158
  arity = get_irn_arity(node);
  for (i = 0; i < arity; i++) {
159
    pred = get_irn_n(node, i);
160
    if (get_nodes_block(pred) == from_bl)
161
162
163
164
165
166
167
168
169
      move(pred, from_bl, to_bl);
  }
}

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

170
  /* Turn off optimizations so that blocks are not merged again. */
171
  int rem_opt = get_opt_optimize();
172
173
  set_optimize(0);

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

  /* 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) {
193
    if(get_nodes_block(phi) == old_block);   /* @@@ inlinening chokes on phis that don't
Florian Liekweg's avatar
Florian Liekweg committed
194
195
                           obey this condition.  How do they get into
                           the list??? Example: InterfaceIII */
196
      set_nodes_block(phi, new_block);
197
198
    phi = get_irn_link(phi);
  }
199
200

  set_optimize(rem_opt);
201
}