summaryrefslogtreecommitdiff
path: root/chill/src/irtools.cc
blob: 4ab6c85a6bdc9daac615d6b1992ace011b73fdb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*****************************************************************************
 Copyright (C) 2010 University of Utah
 All Rights Reserved.

 Purpose:
   Useful tools to analyze code in compiler IR format.

 Notes:

 History:
   06/2010 Created by Chun Chen.
*****************************************************************************/

#include <iostream>
#include <code_gen/CG_outputBuilder.h>
#include "irtools.hh"
#include "omegatools.hh"
#include "chill_error.hh"

using namespace omega;

// Build IR tree from the source code.  Block type node can only be
// leaf, i.e., there is no further structures inside a block allowed.
std::vector<ir_tree_node *> build_ir_tree(IR_Control *control, ir_tree_node *parent) {
  std::vector<ir_tree_node *> result;
  
  switch (control->type()) {
  case IR_CONTROL_BLOCK: {
    std::vector<IR_Control *> controls = control->ir_->FindOneLevelControlStructure(static_cast<IR_Block *>(control));
    if (controls.size() == 0) {
      ir_tree_node *node = new ir_tree_node;
      node->content = control;
      node->parent = parent;
      node->payload = -1;
      result.push_back(node);
    }
    else {
      delete control;
      for (int i = 0; i < controls.size(); i++)
        switch (controls[i]->type()) {
        case IR_CONTROL_BLOCK: {
          std::vector<ir_tree_node *> t = build_ir_tree(controls[i], parent);
          result.insert(result.end(), t.begin(), t.end());
          break;
        }
        case IR_CONTROL_LOOP: {
          ir_tree_node *node = new ir_tree_node;
          node->content = controls[i];
          node->parent = parent;
          node->children = build_ir_tree(static_cast<IR_Loop *>(controls[i])->body(), node);
          node->payload = -1;
          result.push_back(node);
          break;
        }
        case IR_CONTROL_IF: {
          static int unique_if_identifier = 0;
          
          IR_Block *block = static_cast<IR_If *>(controls[i])->then_body();
          if (block != NULL) {
            ir_tree_node *node = new ir_tree_node;
            node->content = controls[i];
            node->parent = parent;
            node->children = build_ir_tree(block, node);
            node->payload = unique_if_identifier+1;
            result.push_back(node);
          }
          
          
          block = static_cast<IR_If *>(controls[i])->else_body();
          if ( block != NULL) {
            ir_tree_node *node = new ir_tree_node;
            node->content = controls[i]->clone();
            node->parent = parent;
            node->children = build_ir_tree(block, node);
            node->payload = unique_if_identifier;
            result.push_back(node);
          }
          
          unique_if_identifier += 2;
          break;
        }
        default:
          ir_tree_node *node = new ir_tree_node;
          node->content = controls[i];
          node->parent = parent;
          node->payload = -1;
          result.push_back(node);
          break;
        }
    }
    break;
  }
  case IR_CONTROL_LOOP: {
    ir_tree_node *node = new ir_tree_node;
    node->content = control;
    node->parent = parent;
    node->children = build_ir_tree(static_cast<const IR_Loop *>(control)->body(), node);
    node->payload = -1;
    result.push_back(node);
    break;
  }
  default:
    ir_tree_node *node = new ir_tree_node;
    node->content = control;
    node->parent = parent;
    node->payload = -1;
    result.push_back(node);
    break;
  }
  
  return result;
}


// Extract statements from IR tree. Statements returned are ordered in
// lexical order in the source code.
std::vector<ir_tree_node *> extract_ir_stmts(const std::vector<ir_tree_node *> &ir_tree) {
  std::vector<ir_tree_node *> result;
  for (int i = 0; i < ir_tree.size(); i++)
    switch (ir_tree[i]->content->type()) {
    case IR_CONTROL_BLOCK:
      result.push_back(ir_tree[i]);
      break;
    case IR_CONTROL_LOOP: {
      // clear loop payload from previous unsuccessful initialization process
      ir_tree[i]->payload = -1;
      
      std::vector<ir_tree_node *> t = extract_ir_stmts(ir_tree[i]->children);
      result.insert(result.end(), t.begin(), t.end());
      break;
    }      
    case IR_CONTROL_IF: {
      std::vector<ir_tree_node *> t = extract_ir_stmts(ir_tree[i]->children);
      result.insert(result.end(), t.begin(), t.end());
      break;
    }
    default:
      throw std::invalid_argument("invalid ir tree");
    }
  
  return result;
}


bool is_dependence_valid(ir_tree_node *src_node, ir_tree_node *dst_node,
                         const DependenceVector &dv, bool before) {
  std::set<ir_tree_node *> loop_nodes;
  ir_tree_node *itn = src_node;
  
  if (!dv.is_scalar_dependence) {
    while (itn->parent != NULL) {
      itn = itn->parent;
      if (itn->content->type() == IR_CONTROL_LOOP)
        loop_nodes.insert(itn);
    }
    
    int last_dim = -1;
    itn = dst_node;
    while (itn->parent != NULL) {
      itn = itn->parent;
      if (itn->content->type() == IR_CONTROL_LOOP
          && loop_nodes.find(itn) != loop_nodes.end()
          && itn->payload > last_dim)
        last_dim = itn->payload;
    }
    
    if (last_dim == -1)
      return true;
    
    for (int i = 0; i <= last_dim; i++) {
      if (dv.lbounds[i] > 0)
        return true;
      else if (dv.lbounds[i] < 0)
        return false;
    }
    
    if (before)
      return true;
    else
      return false;
  }
  
  return true;
  
}



// Test data dependences between two statements. The first statement
// in parameter must be lexically before the second statement in
// parameter.  Returned dependences are all lexicographically
// positive. The first vector in returned pair is dependences from the
// first statement to the second statement and the second vector in
// returned pair is in reverse order.
std::pair<std::vector<DependenceVector>, std::vector<DependenceVector> > test_data_dependences(
  IR_Code *ir, const CG_outputRepr *repr1, const Relation &IS1,
  const CG_outputRepr *repr2, const Relation &IS2,
  std::vector<Free_Var_Decl*> &freevar, std::vector<std::string> index,
  int i, int j) {
  std::pair<std::vector<DependenceVector>, std::vector<DependenceVector> > result;
  
  if (repr1 == repr2) {
    std::vector<IR_ArrayRef *> access = ir->FindArrayRef(repr1);
    
    for (int i = 0; i < access.size(); i++) {
      IR_ArrayRef *a = access[i];
      IR_ArraySymbol *sym_a = a->symbol();
      for (int j = i; j < access.size(); j++) {
        IR_ArrayRef *b = access[j];
        IR_ArraySymbol *sym_b = b->symbol();
        
        if (*sym_a == *sym_b && (a->is_write() || b->is_write())) {
          Relation r = arrays2relation(ir, freevar, a, IS1, b, IS2);
          std::pair<std::vector<DependenceVector>,
            std::vector<DependenceVector> > dv =
            relation2dependences(a, b, r);
          result.first.insert(result.first.end(), dv.first.begin(),
                              dv.first.end());
          result.second.insert(result.second.end(), dv.second.begin(),
                               dv.second.end());
        }
        delete sym_b;
      }
      delete sym_a;
      
    }
    
    for (int i = 0; i < access.size(); i++)
      delete access[i];
  } else {
    std::vector<IR_ArrayRef *> access1 = ir->FindArrayRef(repr1);
    std::vector<IR_ArrayRef *> access2 = ir->FindArrayRef(repr2);
    
    for (int i = 0; i < access1.size(); i++) {
      IR_ArrayRef *a = access1[i];
      IR_ArraySymbol *sym_a = a->symbol();
      
      for (int j = 0; j < access2.size(); j++) {
        IR_ArrayRef *b = access2[j];
        IR_ArraySymbol *sym_b = b->symbol();
        if (*sym_a == *sym_b && (a->is_write() || b->is_write())) {
          Relation r = arrays2relation(ir, freevar, a, IS1, b, IS2);
          std::pair<std::vector<DependenceVector>,
            std::vector<DependenceVector> > dv =
            relation2dependences(a, b, r);
          
          result.first.insert(result.first.end(), dv.first.begin(),
                              dv.first.end());
          result.second.insert(result.second.end(), dv.second.begin(),
                               dv.second.end());
        }
        delete sym_b;
      }
      delete sym_a;
    }
    
    for (int i = 0; i < access1.size(); i++)
      delete access1[i];
    for (int i = 0; i < access2.size(); i++)
      delete access2[i];
  }
  /*std::pair<std::vector<DependenceVector>,
    std::vector<DependenceVector> > dv =
    ir->FindScalarDeps(repr1, repr2, index, i, j);
    
    
    result.first.insert(result.first.end(), dv.first.begin(),
    dv.first.end());
    result.second.insert(result.second.end(), dv.second.begin(),
    dv.second.end());*/
  /*result.first.insert(result.first.end(), dv.first.begin(),
    dv.first.end());
    result.second.insert(result.second.end(), dv.second.begin(),
    dv.second.end());
  */
  
  return result;
}