summaryrefslogtreecommitdiff
path: root/omegalib/code_gen/src/code_gen.cc
blob: 168c86b0f94966f94cbff071f0f130c8b022d764 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*****************************************************************************
 Copyright (C) 1994-2000 University of Maryland
 Copyright (C) 2008 University of Southern California
 Copyright (C) 2009-2010 University of Utah
 All Rights Reserved.

 Purpose:
   Start code generation process here.

 Notes:

 History:
   04/24/96 MMGenerateCode implementation, added by D people. Lei Zhou
*****************************************************************************/

#include <omega.h>
#include <omega/Rel_map.h>
#include <basic/Collection.h>
#include <basic/Bag.h>
#include <basic/Map.h>
#include <basic/util.h>
#include <basic/omega_error.h>
#include <math.h>
#include <vector>

#include <code_gen/CG.h>
#include <code_gen/code_gen.h>
#include <code_gen/CG_outputBuilder.h>
#include <code_gen/CG_outputRepr.h>
#include <code_gen/CG_stringBuilder.h>
#include <code_gen/CG_stringRepr.h>
#include <code_gen/output_repr.h>

namespace omega {


int last_level;// Should not be global, but it is.
SetTuple new_IS;
SetTupleTuple projected_nIS;
Tuple<CG_outputRepr *> statementInfo;
RelTuple transformations;

//protonu--adding stuff to make Chun's code work with Gabe's
Tuple< Tuple<int> > smtNonSplitLevels;
Tuple< Tuple<std::string> > loopIdxNames;//per stmt
std::vector< std::pair<int, std::string> > syncs;

//protonu-putting this in for now, not sure what all these do
//This lovely ugly hack allows us to extract hard upper-bounds at
//specific loop levels
int checkLoopLevel;
int stmtForLoopCheck;
int upperBoundForLevel;
int lowerBoundForLevel;
bool fillInBounds;

//trick to static init checkLoopLevel to 0
class JunkStaticInit{ public: JunkStaticInit(){ checkLoopLevel=0; fillInBounds=false;} };
static JunkStaticInit junkInitInstance__;

//end--protonu.


CG_result * gen_recursive(int level, IntTuple &isActive);


int code_gen_debug=0;


SetTuple filter_function_symbols(SetTuple &sets, bool keep_fs){
  SetTuple new_sets(sets.size());
  for(int i = 1; i <= sets.size(); i++) {
    Relation R = sets[i];
    Relation &S = new_sets[i];    
    assert(R.is_set());
    
    S = Relation(R.n_set());
    S.copy_names(R);
    F_Exists *fe = S.add_exists();
    F_Or *fo = fe->add_or();
    for(DNF_Iterator D(R.query_DNF()); D; D++) {
      F_And *fa = fo->add_and();
      Variable_ID_Tuple &oldlocals = (*D)->locals();
      Section<Variable_ID> newlocals = fe->declare_tuple(oldlocals.size());

      /* copy constraints.  This is much more difficult than it needs
         to be, but add_EQ(Constraint_Handle) doesn't work because it can't
         keep track of existentially quantified varaibles across calls.
         Sigh.  */

      for(EQ_Iterator e(*D); e; e++)
        if((max_fs_arity(*e) > 0) == keep_fs){
          EQ_Handle n = fa->add_EQ();
          for(Constr_Vars_Iter cvi(*e,false);cvi;cvi++)
            if((*cvi).var->kind() == Wildcard_Var)
              n.update_coef(newlocals[oldlocals.index((*cvi).var)],
                            (*cvi).coef);
            else
              if((*cvi).var->kind() == Global_Var)
                n.update_coef(S.get_local((*cvi).var->get_global_var(),
                                          (*cvi).var->function_of()),
                              (*cvi).coef);
              else
                n.update_coef((*cvi).var,(*cvi).coef);
          n.update_const((*e).get_const());
          n.finalize();
        }

      for(GEQ_Iterator g(*D); g; g++)
        if((max_fs_arity(*g) > 0) == keep_fs) {
          GEQ_Handle n = fa->add_GEQ();
          for(Constr_Vars_Iter cvi(*g,false);cvi;cvi++)
            if((*cvi).var->kind() == Wildcard_Var)
              n.update_coef(newlocals[oldlocals.index((*cvi).var)],
                            (*cvi).coef);
            else
              if((*cvi).var->kind() == Global_Var)
                n.update_coef(S.get_local((*cvi).var->get_global_var(),
                                          (*cvi).var->function_of()),
                              (*cvi).coef);
              else
                n.update_coef((*cvi).var,(*cvi).coef);
          n.update_const((*g).get_const());
          n.finalize();
        }
    }
    S.finalize();
  }

  return new_sets;
}


RelTuple strip_function_symbols(SetTuple &sets) {
  return filter_function_symbols(sets,false);
}

RelTuple extract_function_symbols(SetTuple &sets) {
  return filter_function_symbols(sets,true);
}


std::string MMGenerateCode(RelTuple &T, SetTuple &old_IS, Relation &known, int effort) {
  Tuple<CG_outputRepr *> nameInfo;
  for (int stmt = 1; stmt <= T.size(); stmt++)
    nameInfo.append(new CG_stringRepr("s" + to_string(stmt)));

  CG_stringBuilder ocg;
  CG_stringRepr *sRepr = static_cast<CG_stringRepr *>(MMGenerateCode(&ocg, T, old_IS, nameInfo, known, effort));

  for (int i = 1; i <= nameInfo.size(); i++)
    delete nameInfo[i];
  if (sRepr != NULL)
    return GetString(sRepr);
  else
    return std::string();
}


//*****************************************************************************
// MMGenerateCode implementation, added by D people. Lei Zhou, Apr. 24, 96
//*****************************************************************************
CG_outputRepr* MMGenerateCode(CG_outputBuilder* ocg, RelTuple &T, SetTuple &old_IS, const Tuple<CG_outputRepr *> &stmt_content, Relation &known, int effort) {
  int stmts = T.size();
  if (stmts == 0)
    return ocg->CreateComment(1, "No statements found!");
  if (!known.is_null())
    known.simplify();
  
  // prepare iteration spaces by splitting disjoint conjunctions
  int maxStmt = 1;
  last_level = 0;
  for (int stmt = 1; stmt <= stmts; stmt++) {
    int old_dim = T[stmt].n_out();
    if (old_dim > last_level)
      last_level = old_dim;

    for (int i = 1; i <= old_IS[stmt].n_set(); i++)
      T[stmt].name_input_var(i, old_IS[stmt].set_var(i)->name());
    for (int i = 1; i <= old_dim; i++)
      T[stmt].name_output_var(i, std::string("t")+to_string(i));
    T[stmt].setup_names();
    
    Relation R = Range(Restrict_Domain(copy(T[stmt]), copy(old_IS[stmt])));
    R.simplify();
    while(R.is_upper_bound_satisfiable()) {
      new_IS.reallocate(maxStmt);
      transformations.reallocate(maxStmt);
      statementInfo.reallocate(maxStmt);
      DNF *dnf = R.query_DNF();
      DNF_Iterator c(dnf);
      Relation R2 = Relation(R, *c);
      R2.simplify();
      if (R2.is_inexact())
        throw codegen_error("unknown constraint in loop bounds");   
      if (known.is_null()) {
        new_IS[maxStmt] = R2;
        transformations[maxStmt] = T[stmt];
        statementInfo[maxStmt] = stmt_content[stmt];
        maxStmt++;
      }
      else {
        Relation R2_extended = copy(R2);
        Relation known_extended = copy(known);
        if (R2.n_set() > known.n_set())
          known_extended = Extend_Set(known_extended, R2.n_set()-known.n_set());
        else if (R2.n_set() < known.n_set())
          R2_extended = Extend_Set(R2_extended, known.n_set()-R2.n_set());
        if (Intersection(R2_extended, known_extended).is_upper_bound_satisfiable()) {
          new_IS[maxStmt] = R2;
          transformations[maxStmt] = T[stmt];
          statementInfo[maxStmt] = stmt_content[stmt];
          maxStmt++;
        }
      }
      c.next();
      if (!c.live()) 
        break;
      if(code_gen_debug) {
        fprintf(DebugFile, "splitting iteration space for disjoint form\n");
        fprintf(DebugFile, "Original iteration space: \n");
        R.print_with_subs(DebugFile);
        fprintf(DebugFile, "First conjunct: \n");
        R2.print_with_subs(DebugFile);
      }
      Relation remainder(R, *c);
      c.next();
      while (c.live()) {
        remainder = Union(remainder, Relation(R, *c));
        c.next();
      }
      R = Difference(remainder, copy(R2));
      R.simplify();
      if(code_gen_debug) {
        fprintf(DebugFile, "Remaining iteration space: \n");
        R.print_with_subs(DebugFile);
      }
    }
  }

  // reset number of statements
  stmts = maxStmt-1;
  if(stmts == 0)
    return ocg->CreateComment(1, "No points in any of the iteration spaces!");

  // entend iteration spaces to maximum dimension
  for (int stmt = 1; stmt <= stmts; stmt++) {
    int old_dim = new_IS[stmt].n_set();
    if (old_dim < last_level) {
      new_IS[stmt] = Extend_Set(new_IS[stmt], last_level-old_dim);
      F_And *f_root = new_IS[stmt].and_with_and();
      for (int i = old_dim+1; i <= last_level; i++) {
        EQ_Handle h = f_root->add_EQ();
        h.update_coef(new_IS[stmt].set_var(i), 1);
        h.update_const(posInfinity);
      }
    }   
  }
  
  // standarize the known condition
  if(known.is_null()) {
    known = Relation::True(last_level);
  }
  known = Extend_Set(known, last_level-known.n_set());
  for (int i = 1; i <= last_level; i++)
    known.name_set_var(i, std::string("t")+to_string(i));
  known.setup_names();
  
  // prepare projected subspaces for each loop level
  projected_nIS.clear();
  projected_nIS.reallocate(last_level);
  for(int i = 1; i <= last_level; i++ ) {
    projected_nIS[i].reallocate(stmts);
  }
  for (int stmt = 1; stmt <= stmts; stmt++) {
    if (last_level > 0)
      projected_nIS[last_level][stmt] = new_IS[stmt];
    for (int i = last_level-1; i >= 1; i--) {
      projected_nIS[i][stmt] = Project(copy(projected_nIS[i+1][stmt]), i+1, Set_Var);
      projected_nIS[i][stmt].simplify();
    }
  }

  // recursively generate AST
  IntTuple allStmts(stmts);
  for(int i = 1; i <= stmts; i++)
    allStmts[i] = 1;
  CG_result *cg = gen_recursive(1, allStmts); 

  // always force finite bounds
  cg = cg->recompute(known, known);
  cg = cg->force_finite_bounds();

  // loop overhead removal based on actual nesting depth -- by chun 09/17/2008
  for (int i = 1; i <= min(effort, cg->depth()); i++)
    cg = cg->liftOverhead(i);

  // merge adjacent if-conditions -- by chun 10/24/2006
  cg->hoistGuard();

  // really print out the loop
  //CG_outputRepr* sRepr = cg->printRepr(ocg, 1, std::vector<CG_outputRepr *>(last_level, NULL));
  CG_outputRepr* sRepr = cg->printRepr(ocg, 1, std::vector<CG_outputRepr *>(last_level));
  delete cg;
  cg = NULL;
  projected_nIS.clear();
  transformations.clear();
  new_IS.clear();

  return sRepr;
}

//protonu--overload the above MMGenerateCode to take into the CUDA-CHiLL
CG_outputRepr* MMGenerateCode(CG_outputBuilder* ocg, RelTuple &T, SetTuple &old_IS, 
		const Tuple<CG_outputRepr *> &stmt_content, Relation &known,
		Tuple< IntTuple >& smtNonSplitLevels_,
	       	std::vector< std::pair<int, std::string> > syncs_,
	       	const Tuple< Tuple<std::string> >& loopIdxNames_, 
	       	int effort) {
  int stmts = T.size();
  if (stmts == 0)
    return ocg->CreateComment(1, "No statements found!");
  if (!known.is_null())
    known.simplify();

  //protonu-- 
  //easier to handle this as a global
  smtNonSplitLevels = smtNonSplitLevels_;
  syncs = syncs_;
  loopIdxNames = loopIdxNames_; 
  //end-protonu



  // prepare iteration spaces by splitting disjoint conjunctions
  int maxStmt = 1;
  last_level = 0;
  for (int stmt = 1; stmt <= stmts; stmt++) {
    int old_dim = T[stmt].n_out();
    if (old_dim > last_level)
      last_level = old_dim;

    for (int i = 1; i <= old_IS[stmt].n_set(); i++)
      T[stmt].name_input_var(i, old_IS[stmt].set_var(i)->name());
    for (int i = 1; i <= old_dim; i++)
      T[stmt].name_output_var(i, std::string("t")+to_string(i));
    T[stmt].setup_names();
    
    Relation R = Range(Restrict_Domain(copy(T[stmt]), copy(old_IS[stmt])));
    R.simplify();
    while(R.is_upper_bound_satisfiable()) {
      new_IS.reallocate(maxStmt);
      transformations.reallocate(maxStmt);
      statementInfo.reallocate(maxStmt);

      //protonu--putting in fix provided by Mark Hall
      smtNonSplitLevels.reallocate(maxStmt);
      //end-protonu


      DNF *dnf = R.query_DNF();
      DNF_Iterator c(dnf);
      Relation R2 = Relation(R, *c);
      R2.simplify();
      if (R2.is_inexact())
        throw codegen_error("unknown constraint in loop bounds");   
      if (known.is_null()) {
        new_IS[maxStmt] = R2;
        transformations[maxStmt] = T[stmt];
        statementInfo[maxStmt] = stmt_content[stmt];
        maxStmt++;
      }
      else {
        Relation R2_extended = copy(R2);
        Relation known_extended = copy(known);
        if (R2.n_set() > known.n_set())
          known_extended = Extend_Set(known_extended, R2.n_set()-known.n_set());
        else if (R2.n_set() < known.n_set())
          R2_extended = Extend_Set(R2_extended, known.n_set()-R2.n_set());
        if (Intersection(R2_extended, known_extended).is_upper_bound_satisfiable()) {
          new_IS[maxStmt] = R2;
          transformations[maxStmt] = T[stmt];
          statementInfo[maxStmt] = stmt_content[stmt];
          maxStmt++;
        }
      }
      c.next();
      if (!c.live()) 
        break;
      if(code_gen_debug) {
        fprintf(DebugFile, "splitting iteration space for disjoint form\n");
        fprintf(DebugFile, "Original iteration space: \n");
        R.print_with_subs(DebugFile);
        fprintf(DebugFile, "First conjunct: \n");
        R2.print_with_subs(DebugFile);
      }
      Relation remainder(R, *c);
      c.next();
      while (c.live()) {
        remainder = Union(remainder, Relation(R, *c));
        c.next();
      }
      R = Difference(remainder, copy(R2));
      R.simplify();
      if(code_gen_debug) {
        fprintf(DebugFile, "Remaining iteration space: \n");
        R.print_with_subs(DebugFile);
      }
    }
  }

  // reset number of statements
  stmts = maxStmt-1;
  if(stmts == 0)
    return ocg->CreateComment(1, "No points in any of the iteration spaces!");

  // entend iteration spaces to maximum dimension
  for (int stmt = 1; stmt <= stmts; stmt++) {
    int old_dim = new_IS[stmt].n_set();
    if (old_dim < last_level) {
      new_IS[stmt] = Extend_Set(new_IS[stmt], last_level-old_dim);
      F_And *f_root = new_IS[stmt].and_with_and();
      for (int i = old_dim+1; i <= last_level; i++) {
        EQ_Handle h = f_root->add_EQ();
        h.update_coef(new_IS[stmt].set_var(i), 1);
        h.update_const(posInfinity);
      }
    }   
  }
  
  // standarize the known condition
  if(known.is_null()) {
    known = Relation::True(last_level);
  }
  known = Extend_Set(known, last_level-known.n_set());
  for (int i = 1; i <= last_level; i++)
    known.name_set_var(i, std::string("t")+to_string(i));
  known.setup_names();
  
  // prepare projected subspaces for each loop level
  projected_nIS.clear();
  projected_nIS.reallocate(last_level);
  for(int i = 1; i <= last_level; i++ ) {
    projected_nIS[i].reallocate(stmts);
  }
  for (int stmt = 1; stmt <= stmts; stmt++) {
    if (last_level > 0)
      projected_nIS[last_level][stmt] = new_IS[stmt];
    for (int i = last_level-1; i >= 1; i--) {
      projected_nIS[i][stmt] = Project(copy(projected_nIS[i+1][stmt]), i+1, Set_Var);
      projected_nIS[i][stmt].simplify();
    }
  }

  // recursively generate AST
  IntTuple allStmts(stmts);
  for(int i = 1; i <= stmts; i++)
    allStmts[i] = 1;
  CG_result *cg = gen_recursive(1, allStmts); 

  // always force finite bounds
  cg = cg->recompute(known, known);
  cg = cg->force_finite_bounds();

  // loop overhead removal based on actual nesting depth -- by chun 09/17/2008
  for (int i = 1; i <= min(effort, cg->depth()); i++)
    cg = cg->liftOverhead(i);

  // merge adjacent if-conditions -- by chun 10/24/2006
  cg->hoistGuard();

  // really print out the loop
  //CG_outputRepr* sRepr = cg->printRepr(ocg, 1, std::vector<CG_outputRepr *>(last_level, NULL));
  CG_outputRepr* sRepr = cg->printRepr(ocg, 1, std::vector<CG_outputRepr *>(last_level ));
  delete cg;
  cg = NULL;
  projected_nIS.clear();
  transformations.clear();
  new_IS.clear();

  return sRepr;
}

CG_result *gen_recursive(int level, IntTuple &isActive) {
  int stmts = isActive.size();

  Set<int> active;
  int s;
  for(s = 1; s <= stmts; s++)
    if(isActive[s]) active.insert(s);

  assert (active.size() >= 1);
  if(level > last_level) return new CG_leaf(isActive);

  if (active.size() == 1)
    return new CG_loop(isActive,level, gen_recursive(level+1,isActive));

  bool constantLevel = true;
   
  int test_rel_size;
  coef_t start,finish; 
  finish = -(posInfinity-1); // -(MAXINT-1);
  start = posInfinity;     // MAXINT;
  Tuple<coef_t> when(stmts);
  for(s=1; s<=stmts; s++) if (isActive[s]) {
      coef_t lb,ub;
      test_rel_size = projected_nIS[level][s].n_set();
      projected_nIS[level][s].single_conjunct()
        ->query_variable_bounds(
          projected_nIS[level][s].set_var(level),
          lb,ub);
      if(code_gen_debug) {
        fprintf(DebugFile, "IS%d:  " coef_fmt " <= t%d <= " coef_fmt "\n",s,
                lb,level,ub);
        projected_nIS[level][s].prefix_print(DebugFile);
      }
      if (lb != ub) {
        constantLevel = false;
        break;
      }
      else {
        set_max(finish,lb);
        set_min(start,lb);
        when[s] = lb;
      }
    }

 
  if (constantLevel && finish-start <= stmts) {
    IntTuple newActive(isActive.size());
    for(int i=1; i<=stmts; i++)  
      newActive[i] = isActive[i] && when[i] == start;
    CG_result *r  = new CG_loop(isActive,level, 
                                gen_recursive(level+1,newActive));
    for(coef_t time = start+1; time <= finish; time++) {
      int count = 0;
      for(int i=1; i<=stmts; i++)   {
        newActive[i] = isActive[i] && when[i] == time;
        if (newActive[i]) count++;
      }
      if (count) {
        Relation test_rel(test_rel_size);
        GEQ_Handle g = test_rel.and_with_GEQ(); 
        g.update_coef(test_rel.set_var(level),-1);
        g.update_const(time-1);
   
        r = new CG_split(isActive,level,test_rel,r,
                         new CG_loop(isActive,level, 
                                     gen_recursive(level+1,newActive)));
      }  
    }
    return r;
  }
  
// Since the Hull computation is approximate, we will get regions that
// have no stmts.  (since we will have split on constraints on the
// hull, and thus we are looking at a region outside the convex hull
// of all the iteration spaces.)
#if 1
  Relation hull = Hull(projected_nIS[level],isActive,1);
#else
  Relation hull = Hull(projected_nIS[level],isActive,0);
#endif

  if(code_gen_debug) {
    fprintf(DebugFile, "Hull (level %d) is:\n",level);
    hull.prefix_print(DebugFile);
  }

  
  IntTuple firstChunk(isActive);
  IntTuple secondChunk(isActive);

  //protonu-warn Chun about this change
  //This does some fancy splitting of statements into loops with the
  //fewest dimentions, but that's not necessarily what we want when
  //code-gening for CUDA. smtNonSplitLevels keeps track per-statment of
  //the levels that should not be split on.
  bool checkForSplits = true;
  for (int s = 1; s <= isActive.size(); s++){
    if (isActive[s]) {
      if(s < smtNonSplitLevels.size() && smtNonSplitLevels[s].index(level-2) != 0){
        checkForSplits = false;
        break;
      }
    }
  }

  //protonu-modifying the next for loop
  for (int s = 1; checkForSplits && s <= isActive.size(); s++)
    if (isActive[s]) {
      Relation gist = Gist(copy(projected_nIS[level][s]),copy(hull),1);
      if (gist.is_obvious_tautology()) break;
      gist.simplify();
      Conjunct *s_conj = gist.single_conjunct();
      for(GEQ_Iterator G(s_conj); G; G++) {
        Relation test_rel(gist.n_set());
        test_rel.and_with_GEQ(*G);
        Variable_ID v = set_var(level);
        coef_t sign = (*G).get_coef(v);
        if(sign > 0) test_rel = Complement(test_rel);
        if(code_gen_debug) {
          fprintf(DebugFile, "Considering split from stmt %d:\n",s);
          test_rel.prefix_print(DebugFile);
        }
  
        firstChunk[s] = sign <= 0;
        secondChunk[s] = sign > 0;
        int numberFirst = sign <= 0;
        int numberSecond = sign > 0;
        
        for (int s2 = 1; s2 <= isActive.size(); s2++)
          if (isActive[s2] && s2 != s) {
            if(code_gen_debug) 
              fprintf(DebugFile,"Consider stmt %d\n",s2);
            bool t = Intersection(copy(projected_nIS[level][s2]),
                                  copy(test_rel)).is_upper_bound_satisfiable();
            bool f = Difference(copy(projected_nIS[level][s2]),
                                copy(test_rel)).is_upper_bound_satisfiable();
            assert(t || f);
            if(code_gen_debug  && t&&f) 
              fprintf(DebugFile, "Slashes stmt %d\n",s2);
            if (t&&f) goto nextGEQ;
            if(code_gen_debug) {
              if (t)
                fprintf(DebugFile, "true for stmt %d\n",s2);
              else 
                fprintf(DebugFile, "false for stmt %d\n",s2);
            }
            if (t) numberFirst++;
            else numberSecond++;
            firstChunk[s2] = t;
            secondChunk[s2] = !t;
          }
            
        assert(numberFirst+numberSecond>1 && "Can't handle wildcard in iteration space");
        if(code_gen_debug) 
          fprintf(DebugFile, "%d true, %d false\n",
                  numberFirst,
                  numberSecond);
        if (numberFirst && numberSecond) {
          // Found a dividing constraint
          return new CG_split(isActive,level,test_rel,
                              gen_recursive(level,firstChunk),
                              gen_recursive(level,secondChunk));
        }
      nextGEQ: ;
      }
    }

  // No way found to divide stmts without splitting, generate loop

  return new CG_loop(isActive,level, gen_recursive(level+1,isActive));
}

}