summaryrefslogtreecommitdiff
path: root/lib/Target/R600/AMDGPUStructurizeCFG.cpp
blob: d14428fd2d71c7f0ce4eb5adf10ea9f28eb382dc (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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//===-- AMDGPUStructurizeCFG.cpp -  ------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// \file
/// The pass implemented in this file transforms the programs control flow
/// graph into a form that's suitable for code generation on hardware that
/// implements control flow by execution masking. This currently includes all
/// AMD GPUs but may as well be useful for other types of hardware.
//
//===----------------------------------------------------------------------===//

#include "AMDGPU.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/RegionIterator.h"
#include "llvm/Analysis/RegionPass.h"
#include "llvm/Module.h"
#include "llvm/Transforms/Utils/SSAUpdater.h"

using namespace llvm;

namespace {

// Definition of the complex types used in this pass.

typedef std::pair<BasicBlock *, Value *> BBValuePair;
typedef ArrayRef<BasicBlock*> BBVecRef;

typedef SmallVector<RegionNode*, 8> RNVector;
typedef SmallVector<BasicBlock*, 8> BBVector;
typedef SmallVector<BBValuePair, 2> BBValueVector;

typedef DenseMap<PHINode *, BBValueVector> PhiMap;
typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
typedef DenseMap<BasicBlock *, Value *> BBPredicates;
typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
typedef DenseMap<BasicBlock *, unsigned> VisitedMap;

// The name for newly created blocks.

static const char *FlowBlockName = "Flow";

/// @brief Transforms the control flow graph on one single entry/exit region
/// at a time.
///
/// After the transform all "If"/"Then"/"Else" style control flow looks like
/// this:
///
/// \verbatim
/// 1
/// ||
/// | |
/// 2 |
/// | /
/// |/   
/// 3
/// ||   Where:
/// | |  1 = "If" block, calculates the condition
/// 4 |  2 = "Then" subregion, runs if the condition is true
/// | /  3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
/// |/   4 = "Else" optional subregion, runs if the condition is false
/// 5    5 = "End" block, also rejoins the control flow
/// \endverbatim
///
/// Control flow is expressed as a branch where the true exit goes into the
/// "Then"/"Else" region, while the false exit skips the region
/// The condition for the optional "Else" region is expressed as a PHI node.
/// The incomming values of the PHI node are true for the "If" edge and false
/// for the "Then" edge.
///
/// Additionally to that even complicated loops look like this:
///
/// \verbatim
/// 1
/// ||
/// | |
/// 2 ^  Where:
/// | /  1 = "Entry" block
/// |/   2 = "Loop" optional subregion, with all exits at "Flow" block
/// 3    3 = "Flow" block, with back edge to entry block
/// |
/// \endverbatim
///
/// The back edge of the "Flow" block is always on the false side of the branch
/// while the true side continues the general flow. So the loop condition
/// consist of a network of PHI nodes where the true incoming values expresses
/// breaks and the false values expresses continue states.
class AMDGPUStructurizeCFG : public RegionPass {

  static char ID;

  Type *Boolean;
  ConstantInt *BoolTrue;
  ConstantInt *BoolFalse;
  UndefValue *BoolUndef;

  Function *Func;
  Region *ParentRegion;

  DominatorTree *DT;

  RNVector Order;
  VisitedMap Visited;
  PredMap Predicates;
  BBPhiMap DeletedPhis;
  BBVector FlowsInserted;

  BasicBlock *LoopStart;
  BasicBlock *LoopEnd;
  BBPredicates LoopPred;

  void orderNodes();

  void buildPredicate(BranchInst *Term, unsigned Idx,
                      BBPredicates &Pred, bool Invert);

  void analyzeBlock(BasicBlock *BB);

  void analyzeLoop(BasicBlock *BB, unsigned &LoopIdx);

  void collectInfos();

  bool dominatesPredicates(BasicBlock *A, BasicBlock *B);

  void killTerminator(BasicBlock *BB);

  RegionNode *skipChained(RegionNode *Node);

  void delPhiValues(BasicBlock *From, BasicBlock *To);

  void addPhiValues(BasicBlock *From, BasicBlock *To);

  BasicBlock *getNextFlow(BasicBlock *Prev);

  bool isPredictableTrue(BasicBlock *Prev, BasicBlock *Node);

  BasicBlock *wireFlowBlock(BasicBlock *Prev, RegionNode *Node);

  void createFlow();

  void insertConditions();

  void rebuildSSA();

public:
  AMDGPUStructurizeCFG():
    RegionPass(ID) {

    initializeRegionInfoPass(*PassRegistry::getPassRegistry());
  }

  virtual bool doInitialization(Region *R, RGPassManager &RGM);

  virtual bool runOnRegion(Region *R, RGPassManager &RGM);

  virtual const char *getPassName() const {
    return "AMDGPU simplify control flow";
  }

  void getAnalysisUsage(AnalysisUsage &AU) const {

    AU.addRequired<DominatorTree>();
    AU.addPreserved<DominatorTree>();
    RegionPass::getAnalysisUsage(AU);
  }

};

} // end anonymous namespace

char AMDGPUStructurizeCFG::ID = 0;

/// \brief Initialize the types and constants used in the pass
bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
  LLVMContext &Context = R->getEntry()->getContext();

  Boolean = Type::getInt1Ty(Context);
  BoolTrue = ConstantInt::getTrue(Context);
  BoolFalse = ConstantInt::getFalse(Context);
  BoolUndef = UndefValue::get(Boolean);

  return false;
}

/// \brief Build up the general order of nodes
void AMDGPUStructurizeCFG::orderNodes() {
  scc_iterator<Region *> I = scc_begin(ParentRegion),
                         E = scc_end(ParentRegion);
  for (Order.clear(); I != E; ++I) {
    std::vector<RegionNode *> &Nodes = *I;
    Order.append(Nodes.begin(), Nodes.end());
  }
}

/// \brief Build blocks and loop predicates
void AMDGPUStructurizeCFG::buildPredicate(BranchInst *Term, unsigned Idx,
                                          BBPredicates &Pred, bool Invert) {
  Value *True = Invert ? BoolFalse : BoolTrue;
  Value *False = Invert ? BoolTrue : BoolFalse;

  RegionInfo *RI = ParentRegion->getRegionInfo();
  BasicBlock *BB = Term->getParent();

  // Handle the case where multiple regions start at the same block
  Region *R = BB != ParentRegion->getEntry() ?
              RI->getRegionFor(BB) : ParentRegion;

  if (R == ParentRegion) {
    // It's a top level block in our region
    Value *Cond = True;
    if (Term->isConditional()) {
      BasicBlock *Other = Term->getSuccessor(!Idx);

      if (Visited.count(Other)) {
        if (!Pred.count(Other))
          Pred[Other] = False;

        if (!Pred.count(BB))
          Pred[BB] = True;
        return;
      }
      Cond = Term->getCondition();

      if (Idx != Invert)
        Cond = BinaryOperator::CreateNot(Cond, "", Term);
    }

    Pred[BB] = Cond;

  } else if (ParentRegion->contains(R)) {
    // It's a block in a sub region
    while(R->getParent() != ParentRegion)
      R = R->getParent();

    Pred[R->getEntry()] = True;

  } else {
    // It's a branch from outside into our parent region
    Pred[BB] = True;
  }
}

/// \brief Analyze the successors of each block and build up predicates
void AMDGPUStructurizeCFG::analyzeBlock(BasicBlock *BB) {
  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
  BBPredicates &Pred = Predicates[BB];

  for (; PI != PE; ++PI) {
    BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());

    for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
      BasicBlock *Succ = Term->getSuccessor(i);
      if (Succ != BB)
        continue;
      buildPredicate(Term, i, Pred, false);
    }
  }
}

/// \brief Analyze the conditions leading to loop to a previous block
void AMDGPUStructurizeCFG::analyzeLoop(BasicBlock *BB, unsigned &LoopIdx) {
  BranchInst *Term = cast<BranchInst>(BB->getTerminator());

  for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
    BasicBlock *Succ = Term->getSuccessor(i);

    // Ignore it if it's not a back edge
    if (!Visited.count(Succ))
      continue;

    buildPredicate(Term, i, LoopPred, true);

    LoopEnd = BB;
    if (Visited[Succ] < LoopIdx) {
      LoopIdx = Visited[Succ];
      LoopStart = Succ;
    }
  }
}

/// \brief Collect various loop and predicate infos
void AMDGPUStructurizeCFG::collectInfos() {
  unsigned Number = 0, LoopIdx = ~0;

  // Reset predicate
  Predicates.clear();

  // and loop infos
  LoopStart = LoopEnd = 0;
  LoopPred.clear();

  RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
  for (Visited.clear(); OI != OE; Visited[(*OI++)->getEntry()] = ++Number) {

    // Analyze all the conditions leading to a node
    analyzeBlock((*OI)->getEntry());

    if ((*OI)->isSubRegion())
      continue;

    // Find the first/last loop nodes and loop predicates
    analyzeLoop((*OI)->getNodeAs<BasicBlock>(), LoopIdx);
  }
}

/// \brief Does A dominate all the predicates of B ?
bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *A, BasicBlock *B) {
  BBPredicates &Preds = Predicates[B];
  for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
       PI != PE; ++PI) {

    if (!DT->dominates(A, PI->first))
      return false;
  }
  return true;
}

/// \brief Remove phi values from all successors and the remove the terminator.
void AMDGPUStructurizeCFG::killTerminator(BasicBlock *BB) {
  TerminatorInst *Term = BB->getTerminator();
  if (!Term)
    return;

  for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
       SI != SE; ++SI) {

    delPhiValues(BB, *SI);
  }

  Term->eraseFromParent();
}

/// First: Skip forward to the first region node that either isn't a subregion or not
/// dominating it's exit, remove all the skipped nodes from the node order.
///
/// Second: Handle the first successor directly if the resulting nodes successor
/// predicates are still dominated by the original entry
RegionNode *AMDGPUStructurizeCFG::skipChained(RegionNode *Node) {
  BasicBlock *Entry = Node->getEntry();

  // Skip forward as long as it is just a linear flow
  while (true) {
    BasicBlock *Entry = Node->getEntry();
    BasicBlock *Exit;

    if (Node->isSubRegion()) {
      Exit = Node->getNodeAs<Region>()->getExit();
    } else {
      TerminatorInst *Term = Entry->getTerminator();
      if (Term->getNumSuccessors() != 1)
        break;
      Exit = Term->getSuccessor(0);
    }

    // It's a back edge, break here so we can insert a loop node
    if (!Visited.count(Exit))
      return Node;

    // More than node edges are pointing to exit
    if (!DT->dominates(Entry, Exit))
      return Node;

    RegionNode *Next = ParentRegion->getNode(Exit);
    RNVector::iterator I = std::find(Order.begin(), Order.end(), Next);
    assert(I != Order.end());

    Visited.erase(Next->getEntry());
    Order.erase(I);
    Node = Next;
  }

  BasicBlock *BB = Node->getEntry();
  TerminatorInst *Term = BB->getTerminator();
  if (Term->getNumSuccessors() != 2)
    return Node;

  // Our node has exactly two succesors, check if we can handle
  // any of them directly
  BasicBlock *Succ = Term->getSuccessor(0);
  if (!Visited.count(Succ) || !dominatesPredicates(Entry, Succ)) {
    Succ = Term->getSuccessor(1);
    if (!Visited.count(Succ) || !dominatesPredicates(Entry, Succ))
      return Node;
  } else {
    BasicBlock *Succ2 = Term->getSuccessor(1);
    if (Visited.count(Succ2) && Visited[Succ] > Visited[Succ2] &&
        dominatesPredicates(Entry, Succ2))
      Succ = Succ2;
  }

  RegionNode *Next = ParentRegion->getNode(Succ);
  RNVector::iterator E = Order.end();
  RNVector::iterator I = std::find(Order.begin(), E, Next);
  assert(I != E);

  killTerminator(BB);
  FlowsInserted.push_back(BB);
  Visited.erase(Succ);
  Order.erase(I);
  return ParentRegion->getNode(wireFlowBlock(BB, Next));
}

/// \brief Remove all PHI values coming from "From" into "To" and remember
/// them in DeletedPhis
void AMDGPUStructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
  PhiMap &Map = DeletedPhis[To];
  for (BasicBlock::iterator I = To->begin(), E = To->end();
       I != E && isa<PHINode>(*I);) {

    PHINode &Phi = cast<PHINode>(*I++);
    while (Phi.getBasicBlockIndex(From) != -1) {
      Value *Deleted = Phi.removeIncomingValue(From, false);
      Map[&Phi].push_back(std::make_pair(From, Deleted));
    }
  }
}

/// \brief Add the PHI values back once we knew the new predecessor
void AMDGPUStructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
  if (!DeletedPhis.count(To))
    return;

  PhiMap &Map = DeletedPhis[To];
  SSAUpdater Updater;

  for (PhiMap::iterator I = Map.begin(), E = Map.end(); I != E; ++I) {

    PHINode *Phi = I->first;
    Updater.Initialize(Phi->getType(), "");
    BasicBlock *Fallback = To;
    bool HaveFallback = false;

    for (BBValueVector::iterator VI = I->second.begin(), VE = I->second.end();
         VI != VE; ++VI) {

      Updater.AddAvailableValue(VI->first, VI->second);
      BasicBlock *Dom = DT->findNearestCommonDominator(Fallback, VI->first);
      if (Dom == VI->first)
        HaveFallback = true;
      else if (Dom != Fallback)
        HaveFallback = false;
      Fallback = Dom;
    }
    if (!HaveFallback) {
      Value *Undef = UndefValue::get(Phi->getType());
      Updater.AddAvailableValue(Fallback, Undef);
    }

    Phi->addIncoming(Updater.GetValueAtEndOfBlock(From), From);
  }
  DeletedPhis.erase(To);
}

/// \brief Create a new flow node and update dominator tree and region info
BasicBlock *AMDGPUStructurizeCFG::getNextFlow(BasicBlock *Prev) {
  LLVMContext &Context = Func->getContext();
  BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
                       Order.back()->getEntry();
  BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
                                        Func, Insert);
  DT->addNewBlock(Flow, Prev);
  ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
  FlowsInserted.push_back(Flow);
  return Flow;
}

/// \brief Can we predict that this node will always be called?
bool AMDGPUStructurizeCFG::isPredictableTrue(BasicBlock *Prev,
                                             BasicBlock *Node) {
  BBPredicates &Preds = Predicates[Node];
  bool Dominated = false;

  for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
       I != E; ++I) {

    if (I->second != BoolTrue)
      return false;

    if (!Dominated && DT->dominates(I->first, Prev))
      Dominated = true;
  }
  return Dominated;
}

/// \brief Wire up the new control flow by inserting or updating the branch
/// instructions at node exits
BasicBlock *AMDGPUStructurizeCFG::wireFlowBlock(BasicBlock *Prev,
                                                RegionNode *Node) {
  BasicBlock *Entry = Node->getEntry();

  if (LoopStart == Entry) {
    LoopStart = Prev;
    LoopPred[Prev] = BoolTrue;
  }

  // Wire it up temporary, skipChained may recurse into us
  BranchInst::Create(Entry, Prev);
  DT->changeImmediateDominator(Entry, Prev);
  addPhiValues(Prev, Entry);

  Node = skipChained(Node);

  BasicBlock *Next = getNextFlow(Prev);
  if (!isPredictableTrue(Prev, Entry)) {
    // Let Prev point to entry and next block
    Prev->getTerminator()->eraseFromParent();
    BranchInst::Create(Entry, Next, BoolUndef, Prev);
  } else {
    DT->changeImmediateDominator(Next, Entry);
  }

  // Let node exit(s) point to next block
  if (Node->isSubRegion()) {
    Region *SubRegion = Node->getNodeAs<Region>();
    BasicBlock *Exit = SubRegion->getExit();

    // Find all the edges from the sub region to the exit
    BBVector ToDo;
    for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
      if (SubRegion->contains(*I))
        ToDo.push_back(*I);
    }

    // Modify the edges to point to the new flow block
    for (BBVector::iterator I = ToDo.begin(), E = ToDo.end(); I != E; ++I) {
      delPhiValues(*I, Exit);
      TerminatorInst *Term = (*I)->getTerminator();
      Term->replaceUsesOfWith(Exit, Next);
    }

    // Update the region info
    SubRegion->replaceExit(Next);

  } else {
    BasicBlock *BB = Node->getNodeAs<BasicBlock>();
    killTerminator(BB);
    BranchInst::Create(Next, BB);

    if (BB == LoopEnd)
      LoopEnd = 0;
  }

  return Next;
}

/// Destroy node order and visited map, build up flow order instead.
/// After this function control flow looks like it should be, but
/// branches only have undefined conditions.
void AMDGPUStructurizeCFG::createFlow() {
  DeletedPhis.clear();

  BasicBlock *Prev = Order.pop_back_val()->getEntry();
  assert(Prev == ParentRegion->getEntry() && "Incorrect node order!");
  Visited.erase(Prev);

  if (LoopStart == Prev) {
    // Loop starts at entry, split entry so that we can predicate it
    BasicBlock::iterator Insert = Prev->getFirstInsertionPt();
    BasicBlock *Split = Prev->splitBasicBlock(Insert, FlowBlockName);
    DT->addNewBlock(Split, Prev);
    ParentRegion->getRegionInfo()->setRegionFor(Split, ParentRegion);
    Predicates[Split] = Predicates[Prev];
    Order.push_back(ParentRegion->getBBNode(Split));
    LoopPred[Prev] = BoolTrue;

  } else if (LoopStart == Order.back()->getEntry()) {
    // Loop starts behind entry, split entry so that we can jump to it
    Instruction *Term = Prev->getTerminator();
    BasicBlock *Split = Prev->splitBasicBlock(Term, FlowBlockName);
    DT->addNewBlock(Split, Prev);
    ParentRegion->getRegionInfo()->setRegionFor(Split, ParentRegion);
    Prev = Split;
  }

  killTerminator(Prev);
  FlowsInserted.clear();
  FlowsInserted.push_back(Prev);

  while (!Order.empty()) {
    RegionNode *Node = Order.pop_back_val();
    Visited.erase(Node->getEntry());
    Prev = wireFlowBlock(Prev, Node);
    if (LoopStart && !LoopEnd) {
      // Create an extra loop end node
      LoopEnd = Prev;
      Prev = getNextFlow(LoopEnd);
      BranchInst::Create(Prev, LoopStart, BoolUndef, LoopEnd);
      addPhiValues(LoopEnd, LoopStart);
    }
  }

  BasicBlock *Exit = ParentRegion->getExit();
  BranchInst::Create(Exit, Prev);
  addPhiValues(Prev, Exit);
  if (DT->dominates(ParentRegion->getEntry(), Exit))
    DT->changeImmediateDominator(Exit, Prev);

  if (LoopStart && LoopEnd) {
    BBVector::iterator FI = std::find(FlowsInserted.begin(),
                                      FlowsInserted.end(),
                                      LoopStart);
    for (; *FI != LoopEnd; ++FI) {
      addPhiValues(*FI, (*FI)->getTerminator()->getSuccessor(0));
    }
  }

  assert(Order.empty());
  assert(Visited.empty());
  assert(DeletedPhis.empty());
}

/// \brief Insert the missing branch conditions
void AMDGPUStructurizeCFG::insertConditions() {
  SSAUpdater PhiInserter;

  for (BBVector::iterator FI = FlowsInserted.begin(), FE = FlowsInserted.end();
       FI != FE; ++FI) {

    BranchInst *Term = cast<BranchInst>((*FI)->getTerminator());
    if (Term->isUnconditional())
      continue;

    PhiInserter.Initialize(Boolean, "");
    PhiInserter.AddAvailableValue(&Func->getEntryBlock(), BoolFalse);

    BasicBlock *Succ = Term->getSuccessor(0);
    BBPredicates &Preds = (*FI == LoopEnd) ? LoopPred : Predicates[Succ];
    for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
         PI != PE; ++PI) {

      PhiInserter.AddAvailableValue(PI->first, PI->second);
    }

    Term->setCondition(PhiInserter.GetValueAtEndOfBlock(*FI));
  }
}

/// Handle a rare case where the disintegrated nodes instructions
/// no longer dominate all their uses. Not sure if this is really nessasary
void AMDGPUStructurizeCFG::rebuildSSA() {
  SSAUpdater Updater;
  for (Region::block_iterator I = ParentRegion->block_begin(),
                              E = ParentRegion->block_end();
       I != E; ++I) {

    BasicBlock *BB = *I;
    for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
         II != IE; ++II) {

      bool Initialized = false;
      for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {

        Next = I->getNext();

        Instruction *User = cast<Instruction>(I->getUser());
        if (User->getParent() == BB) {
          continue;

        } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
          if (UserPN->getIncomingBlock(*I) == BB)
            continue;
        }

        if (DT->dominates(II, User))
          continue;

        if (!Initialized) {
          Value *Undef = UndefValue::get(II->getType());
          Updater.Initialize(II->getType(), "");
          Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
          Updater.AddAvailableValue(BB, II);
          Initialized = true;
        }
        Updater.RewriteUseAfterInsertions(*I);
      }
    }
  }
}

/// \brief Run the transformation for each region found
bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
  if (R->isTopLevelRegion())
    return false;

  Func = R->getEntry()->getParent();
  ParentRegion = R;

  DT = &getAnalysis<DominatorTree>();

  orderNodes();
  collectInfos();
  createFlow();
  insertConditions();
  rebuildSSA();

  Order.clear();
  Visited.clear();
  Predicates.clear();
  DeletedPhis.clear();
  FlowsInserted.clear();

  return true;
}

/// \brief Create the pass
Pass *llvm::createAMDGPUStructurizeCFGPass() {
  return new AMDGPUStructurizeCFG();
}