blob: 7f3a35eaab3c524976e075acc0852de635946017 [file] [log] [blame]
Tom Stellard6b7d99d2012-12-19 22:10:31 +00001//===-- AMDGPUStructurizeCFG.cpp - ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// The pass implemented in this file transforms the programs control flow
12/// graph into a form that's suitable for code generation on hardware that
13/// implements control flow by execution masking. This currently includes all
14/// AMD GPUs but may as well be useful for other types of hardware.
15//
16//===----------------------------------------------------------------------===//
17
18#include "AMDGPU.h"
Tom Stellard6b7d99d2012-12-19 22:10:31 +000019#include "llvm/ADT/SCCIterator.h"
Tom Stellard6b7d99d2012-12-19 22:10:31 +000020#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000021#include "llvm/Analysis/RegionIterator.h"
Tom Stellard6b7d99d2012-12-19 22:10:31 +000022#include "llvm/Analysis/RegionPass.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Tom Stellard6b7d99d2012-12-19 22:10:31 +000024#include "llvm/Transforms/Utils/SSAUpdater.h"
25
26using namespace llvm;
27
28namespace {
29
30// Definition of the complex types used in this pass.
31
32typedef std::pair<BasicBlock *, Value *> BBValuePair;
33typedef ArrayRef<BasicBlock*> BBVecRef;
34
35typedef SmallVector<RegionNode*, 8> RNVector;
36typedef SmallVector<BasicBlock*, 8> BBVector;
37typedef SmallVector<BBValuePair, 2> BBValueVector;
38
39typedef DenseMap<PHINode *, BBValueVector> PhiMap;
40typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
41typedef DenseMap<BasicBlock *, Value *> BBPredicates;
42typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
43typedef DenseMap<BasicBlock *, unsigned> VisitedMap;
Tom Stellard13cf6cb2013-02-08 22:24:35 +000044typedef DenseMap<BasicBlock *, BBVector> BB2BBVecMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000045
46// The name for newly created blocks.
47
48static const char *FlowBlockName = "Flow";
49
50/// @brief Transforms the control flow graph on one single entry/exit region
51/// at a time.
52///
53/// After the transform all "If"/"Then"/"Else" style control flow looks like
54/// this:
55///
56/// \verbatim
57/// 1
58/// ||
59/// | |
60/// 2 |
61/// | /
62/// |/
63/// 3
64/// || Where:
65/// | | 1 = "If" block, calculates the condition
66/// 4 | 2 = "Then" subregion, runs if the condition is true
67/// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
68/// |/ 4 = "Else" optional subregion, runs if the condition is false
69/// 5 5 = "End" block, also rejoins the control flow
70/// \endverbatim
71///
72/// Control flow is expressed as a branch where the true exit goes into the
73/// "Then"/"Else" region, while the false exit skips the region
74/// The condition for the optional "Else" region is expressed as a PHI node.
75/// The incomming values of the PHI node are true for the "If" edge and false
76/// for the "Then" edge.
77///
78/// Additionally to that even complicated loops look like this:
79///
80/// \verbatim
81/// 1
82/// ||
83/// | |
84/// 2 ^ Where:
85/// | / 1 = "Entry" block
86/// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
87/// 3 3 = "Flow" block, with back edge to entry block
88/// |
89/// \endverbatim
90///
91/// The back edge of the "Flow" block is always on the false side of the branch
92/// while the true side continues the general flow. So the loop condition
93/// consist of a network of PHI nodes where the true incoming values expresses
94/// breaks and the false values expresses continue states.
95class AMDGPUStructurizeCFG : public RegionPass {
96
97 static char ID;
98
99 Type *Boolean;
100 ConstantInt *BoolTrue;
101 ConstantInt *BoolFalse;
102 UndefValue *BoolUndef;
103
104 Function *Func;
105 Region *ParentRegion;
106
107 DominatorTree *DT;
108
109 RNVector Order;
110 VisitedMap Visited;
111 PredMap Predicates;
112 BBPhiMap DeletedPhis;
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000113 BB2BBVecMap AddedPhis;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000114 BBVector FlowsInserted;
115
116 BasicBlock *LoopStart;
117 BasicBlock *LoopEnd;
118 BBPredicates LoopPred;
119
120 void orderNodes();
121
122 void buildPredicate(BranchInst *Term, unsigned Idx,
123 BBPredicates &Pred, bool Invert);
124
125 void analyzeBlock(BasicBlock *BB);
126
127 void analyzeLoop(BasicBlock *BB, unsigned &LoopIdx);
128
129 void collectInfos();
130
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000131 void delPhiValues(BasicBlock *From, BasicBlock *To);
132
133 void addPhiValues(BasicBlock *From, BasicBlock *To);
134
135 void setPhiValues();
136
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000137 bool dominatesPredicates(BasicBlock *A, BasicBlock *B);
138
139 void killTerminator(BasicBlock *BB);
140
141 RegionNode *skipChained(RegionNode *Node);
142
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000143 BasicBlock *getNextFlow(BasicBlock *Prev);
144
145 bool isPredictableTrue(BasicBlock *Prev, BasicBlock *Node);
146
147 BasicBlock *wireFlowBlock(BasicBlock *Prev, RegionNode *Node);
148
149 void createFlow();
150
151 void insertConditions();
152
153 void rebuildSSA();
154
155public:
156 AMDGPUStructurizeCFG():
157 RegionPass(ID) {
158
159 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
160 }
161
162 virtual bool doInitialization(Region *R, RGPassManager &RGM);
163
164 virtual bool runOnRegion(Region *R, RGPassManager &RGM);
165
166 virtual const char *getPassName() const {
167 return "AMDGPU simplify control flow";
168 }
169
170 void getAnalysisUsage(AnalysisUsage &AU) const {
171
172 AU.addRequired<DominatorTree>();
173 AU.addPreserved<DominatorTree>();
174 RegionPass::getAnalysisUsage(AU);
175 }
176
177};
178
179} // end anonymous namespace
180
181char AMDGPUStructurizeCFG::ID = 0;
182
183/// \brief Initialize the types and constants used in the pass
184bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000185 LLVMContext &Context = R->getEntry()->getContext();
186
187 Boolean = Type::getInt1Ty(Context);
188 BoolTrue = ConstantInt::getTrue(Context);
189 BoolFalse = ConstantInt::getFalse(Context);
190 BoolUndef = UndefValue::get(Boolean);
191
192 return false;
193}
194
195/// \brief Build up the general order of nodes
196void AMDGPUStructurizeCFG::orderNodes() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000197 scc_iterator<Region *> I = scc_begin(ParentRegion),
198 E = scc_end(ParentRegion);
199 for (Order.clear(); I != E; ++I) {
200 std::vector<RegionNode *> &Nodes = *I;
201 Order.append(Nodes.begin(), Nodes.end());
202 }
203}
204
205/// \brief Build blocks and loop predicates
206void AMDGPUStructurizeCFG::buildPredicate(BranchInst *Term, unsigned Idx,
207 BBPredicates &Pred, bool Invert) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000208 Value *True = Invert ? BoolFalse : BoolTrue;
209 Value *False = Invert ? BoolTrue : BoolFalse;
210
211 RegionInfo *RI = ParentRegion->getRegionInfo();
212 BasicBlock *BB = Term->getParent();
213
214 // Handle the case where multiple regions start at the same block
215 Region *R = BB != ParentRegion->getEntry() ?
216 RI->getRegionFor(BB) : ParentRegion;
217
218 if (R == ParentRegion) {
219 // It's a top level block in our region
220 Value *Cond = True;
221 if (Term->isConditional()) {
222 BasicBlock *Other = Term->getSuccessor(!Idx);
223
224 if (Visited.count(Other)) {
225 if (!Pred.count(Other))
226 Pred[Other] = False;
227
228 if (!Pred.count(BB))
229 Pred[BB] = True;
230 return;
231 }
232 Cond = Term->getCondition();
233
234 if (Idx != Invert)
235 Cond = BinaryOperator::CreateNot(Cond, "", Term);
236 }
237
238 Pred[BB] = Cond;
239
240 } else if (ParentRegion->contains(R)) {
241 // It's a block in a sub region
242 while(R->getParent() != ParentRegion)
243 R = R->getParent();
244
245 Pred[R->getEntry()] = True;
246
247 } else {
248 // It's a branch from outside into our parent region
249 Pred[BB] = True;
250 }
251}
252
253/// \brief Analyze the successors of each block and build up predicates
254void AMDGPUStructurizeCFG::analyzeBlock(BasicBlock *BB) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000255 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
256 BBPredicates &Pred = Predicates[BB];
257
258 for (; PI != PE; ++PI) {
259 BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
260
261 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
262 BasicBlock *Succ = Term->getSuccessor(i);
263 if (Succ != BB)
264 continue;
265 buildPredicate(Term, i, Pred, false);
266 }
267 }
268}
269
270/// \brief Analyze the conditions leading to loop to a previous block
271void AMDGPUStructurizeCFG::analyzeLoop(BasicBlock *BB, unsigned &LoopIdx) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000272 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
273
274 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
275 BasicBlock *Succ = Term->getSuccessor(i);
276
277 // Ignore it if it's not a back edge
278 if (!Visited.count(Succ))
279 continue;
280
281 buildPredicate(Term, i, LoopPred, true);
282
283 LoopEnd = BB;
284 if (Visited[Succ] < LoopIdx) {
285 LoopIdx = Visited[Succ];
286 LoopStart = Succ;
287 }
288 }
289}
290
291/// \brief Collect various loop and predicate infos
292void AMDGPUStructurizeCFG::collectInfos() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000293 unsigned Number = 0, LoopIdx = ~0;
294
295 // Reset predicate
296 Predicates.clear();
297
298 // and loop infos
299 LoopStart = LoopEnd = 0;
300 LoopPred.clear();
301
302 RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
303 for (Visited.clear(); OI != OE; Visited[(*OI++)->getEntry()] = ++Number) {
304
305 // Analyze all the conditions leading to a node
306 analyzeBlock((*OI)->getEntry());
307
308 if ((*OI)->isSubRegion())
309 continue;
310
311 // Find the first/last loop nodes and loop predicates
312 analyzeLoop((*OI)->getNodeAs<BasicBlock>(), LoopIdx);
313 }
314}
315
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000316/// \brief Remove all PHI values coming from "From" into "To" and remember
317/// them in DeletedPhis
318void AMDGPUStructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
319 PhiMap &Map = DeletedPhis[To];
320 for (BasicBlock::iterator I = To->begin(), E = To->end();
321 I != E && isa<PHINode>(*I);) {
322
323 PHINode &Phi = cast<PHINode>(*I++);
324 while (Phi.getBasicBlockIndex(From) != -1) {
325 Value *Deleted = Phi.removeIncomingValue(From, false);
326 Map[&Phi].push_back(std::make_pair(From, Deleted));
327 }
328 }
329}
330
331/// \brief Add a dummy PHI value as soon as we knew the new predecessor
332void AMDGPUStructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
333 for (BasicBlock::iterator I = To->begin(), E = To->end();
334 I != E && isa<PHINode>(*I);) {
335
336 PHINode &Phi = cast<PHINode>(*I++);
337 Value *Undef = UndefValue::get(Phi.getType());
338 Phi.addIncoming(Undef, From);
339 }
340 AddedPhis[To].push_back(From);
341}
342
343/// \brief Add the real PHI value as soon as everything is set up
344void AMDGPUStructurizeCFG::setPhiValues() {
345
346 SSAUpdater Updater;
347 for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
348 AI != AE; ++AI) {
349
350 BasicBlock *To = AI->first;
351 BBVector &From = AI->second;
352
353 if (!DeletedPhis.count(To))
354 continue;
355
356 PhiMap &Map = DeletedPhis[To];
357 for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
358 PI != PE; ++PI) {
359
360 PHINode *Phi = PI->first;
361 Value *Undef = UndefValue::get(Phi->getType());
362 Updater.Initialize(Phi->getType(), "");
363 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
364 Updater.AddAvailableValue(To, Undef);
365
366 for (BBValueVector::iterator VI = PI->second.begin(),
367 VE = PI->second.end(); VI != VE; ++VI) {
368
369 Updater.AddAvailableValue(VI->first, VI->second);
370 }
371
372 for (BBVector::iterator FI = From.begin(), FE = From.end();
373 FI != FE; ++FI) {
374
375 int Idx = Phi->getBasicBlockIndex(*FI);
376 assert(Idx != -1);
377 Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
378 }
379 }
380
381 DeletedPhis.erase(To);
382 }
383 assert(DeletedPhis.empty());
384}
385
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000386/// \brief Does A dominate all the predicates of B ?
387bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *A, BasicBlock *B) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000388 BBPredicates &Preds = Predicates[B];
389 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
390 PI != PE; ++PI) {
391
392 if (!DT->dominates(A, PI->first))
393 return false;
394 }
395 return true;
396}
397
398/// \brief Remove phi values from all successors and the remove the terminator.
399void AMDGPUStructurizeCFG::killTerminator(BasicBlock *BB) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000400 TerminatorInst *Term = BB->getTerminator();
401 if (!Term)
402 return;
403
404 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
405 SI != SE; ++SI) {
406
407 delPhiValues(BB, *SI);
408 }
409
410 Term->eraseFromParent();
411}
412
413/// First: Skip forward to the first region node that either isn't a subregion or not
414/// dominating it's exit, remove all the skipped nodes from the node order.
415///
416/// Second: Handle the first successor directly if the resulting nodes successor
417/// predicates are still dominated by the original entry
418RegionNode *AMDGPUStructurizeCFG::skipChained(RegionNode *Node) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000419 BasicBlock *Entry = Node->getEntry();
420
421 // Skip forward as long as it is just a linear flow
422 while (true) {
423 BasicBlock *Entry = Node->getEntry();
424 BasicBlock *Exit;
425
426 if (Node->isSubRegion()) {
427 Exit = Node->getNodeAs<Region>()->getExit();
428 } else {
429 TerminatorInst *Term = Entry->getTerminator();
430 if (Term->getNumSuccessors() != 1)
431 break;
432 Exit = Term->getSuccessor(0);
433 }
434
435 // It's a back edge, break here so we can insert a loop node
436 if (!Visited.count(Exit))
437 return Node;
438
439 // More than node edges are pointing to exit
440 if (!DT->dominates(Entry, Exit))
441 return Node;
442
443 RegionNode *Next = ParentRegion->getNode(Exit);
444 RNVector::iterator I = std::find(Order.begin(), Order.end(), Next);
445 assert(I != Order.end());
446
447 Visited.erase(Next->getEntry());
448 Order.erase(I);
449 Node = Next;
450 }
451
452 BasicBlock *BB = Node->getEntry();
453 TerminatorInst *Term = BB->getTerminator();
454 if (Term->getNumSuccessors() != 2)
455 return Node;
456
457 // Our node has exactly two succesors, check if we can handle
458 // any of them directly
459 BasicBlock *Succ = Term->getSuccessor(0);
460 if (!Visited.count(Succ) || !dominatesPredicates(Entry, Succ)) {
461 Succ = Term->getSuccessor(1);
462 if (!Visited.count(Succ) || !dominatesPredicates(Entry, Succ))
463 return Node;
464 } else {
465 BasicBlock *Succ2 = Term->getSuccessor(1);
466 if (Visited.count(Succ2) && Visited[Succ] > Visited[Succ2] &&
467 dominatesPredicates(Entry, Succ2))
468 Succ = Succ2;
469 }
470
471 RegionNode *Next = ParentRegion->getNode(Succ);
472 RNVector::iterator E = Order.end();
473 RNVector::iterator I = std::find(Order.begin(), E, Next);
474 assert(I != E);
475
476 killTerminator(BB);
477 FlowsInserted.push_back(BB);
478 Visited.erase(Succ);
479 Order.erase(I);
480 return ParentRegion->getNode(wireFlowBlock(BB, Next));
481}
482
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000483/// \brief Create a new flow node and update dominator tree and region info
484BasicBlock *AMDGPUStructurizeCFG::getNextFlow(BasicBlock *Prev) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000485 LLVMContext &Context = Func->getContext();
486 BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
487 Order.back()->getEntry();
488 BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
489 Func, Insert);
490 DT->addNewBlock(Flow, Prev);
491 ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
492 FlowsInserted.push_back(Flow);
493 return Flow;
494}
495
496/// \brief Can we predict that this node will always be called?
497bool AMDGPUStructurizeCFG::isPredictableTrue(BasicBlock *Prev,
498 BasicBlock *Node) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000499 BBPredicates &Preds = Predicates[Node];
500 bool Dominated = false;
501
502 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
503 I != E; ++I) {
504
505 if (I->second != BoolTrue)
506 return false;
507
508 if (!Dominated && DT->dominates(I->first, Prev))
509 Dominated = true;
510 }
511 return Dominated;
512}
513
514/// \brief Wire up the new control flow by inserting or updating the branch
515/// instructions at node exits
516BasicBlock *AMDGPUStructurizeCFG::wireFlowBlock(BasicBlock *Prev,
517 RegionNode *Node) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000518 BasicBlock *Entry = Node->getEntry();
519
520 if (LoopStart == Entry) {
521 LoopStart = Prev;
522 LoopPred[Prev] = BoolTrue;
523 }
524
525 // Wire it up temporary, skipChained may recurse into us
526 BranchInst::Create(Entry, Prev);
527 DT->changeImmediateDominator(Entry, Prev);
528 addPhiValues(Prev, Entry);
529
530 Node = skipChained(Node);
531
532 BasicBlock *Next = getNextFlow(Prev);
533 if (!isPredictableTrue(Prev, Entry)) {
534 // Let Prev point to entry and next block
535 Prev->getTerminator()->eraseFromParent();
536 BranchInst::Create(Entry, Next, BoolUndef, Prev);
537 } else {
538 DT->changeImmediateDominator(Next, Entry);
539 }
540
541 // Let node exit(s) point to next block
542 if (Node->isSubRegion()) {
543 Region *SubRegion = Node->getNodeAs<Region>();
544 BasicBlock *Exit = SubRegion->getExit();
545
546 // Find all the edges from the sub region to the exit
547 BBVector ToDo;
548 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
549 if (SubRegion->contains(*I))
550 ToDo.push_back(*I);
551 }
552
553 // Modify the edges to point to the new flow block
554 for (BBVector::iterator I = ToDo.begin(), E = ToDo.end(); I != E; ++I) {
555 delPhiValues(*I, Exit);
556 TerminatorInst *Term = (*I)->getTerminator();
557 Term->replaceUsesOfWith(Exit, Next);
558 }
559
560 // Update the region info
561 SubRegion->replaceExit(Next);
562
563 } else {
564 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
565 killTerminator(BB);
566 BranchInst::Create(Next, BB);
567
568 if (BB == LoopEnd)
569 LoopEnd = 0;
570 }
571
572 return Next;
573}
574
575/// Destroy node order and visited map, build up flow order instead.
576/// After this function control flow looks like it should be, but
577/// branches only have undefined conditions.
578void AMDGPUStructurizeCFG::createFlow() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000579 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000580 AddedPhis.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000581
582 BasicBlock *Prev = Order.pop_back_val()->getEntry();
583 assert(Prev == ParentRegion->getEntry() && "Incorrect node order!");
584 Visited.erase(Prev);
585
586 if (LoopStart == Prev) {
587 // Loop starts at entry, split entry so that we can predicate it
588 BasicBlock::iterator Insert = Prev->getFirstInsertionPt();
589 BasicBlock *Split = Prev->splitBasicBlock(Insert, FlowBlockName);
590 DT->addNewBlock(Split, Prev);
591 ParentRegion->getRegionInfo()->setRegionFor(Split, ParentRegion);
592 Predicates[Split] = Predicates[Prev];
593 Order.push_back(ParentRegion->getBBNode(Split));
594 LoopPred[Prev] = BoolTrue;
595
596 } else if (LoopStart == Order.back()->getEntry()) {
597 // Loop starts behind entry, split entry so that we can jump to it
598 Instruction *Term = Prev->getTerminator();
599 BasicBlock *Split = Prev->splitBasicBlock(Term, FlowBlockName);
600 DT->addNewBlock(Split, Prev);
601 ParentRegion->getRegionInfo()->setRegionFor(Split, ParentRegion);
602 Prev = Split;
603 }
604
605 killTerminator(Prev);
606 FlowsInserted.clear();
607 FlowsInserted.push_back(Prev);
608
609 while (!Order.empty()) {
610 RegionNode *Node = Order.pop_back_val();
611 Visited.erase(Node->getEntry());
612 Prev = wireFlowBlock(Prev, Node);
613 if (LoopStart && !LoopEnd) {
614 // Create an extra loop end node
615 LoopEnd = Prev;
616 Prev = getNextFlow(LoopEnd);
617 BranchInst::Create(Prev, LoopStart, BoolUndef, LoopEnd);
618 addPhiValues(LoopEnd, LoopStart);
619 }
620 }
621
622 BasicBlock *Exit = ParentRegion->getExit();
623 BranchInst::Create(Exit, Prev);
624 addPhiValues(Prev, Exit);
625 if (DT->dominates(ParentRegion->getEntry(), Exit))
626 DT->changeImmediateDominator(Exit, Prev);
627
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000628 assert(Order.empty());
629 assert(Visited.empty());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000630}
631
632/// \brief Insert the missing branch conditions
633void AMDGPUStructurizeCFG::insertConditions() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000634 SSAUpdater PhiInserter;
635
636 for (BBVector::iterator FI = FlowsInserted.begin(), FE = FlowsInserted.end();
637 FI != FE; ++FI) {
638
639 BranchInst *Term = cast<BranchInst>((*FI)->getTerminator());
640 if (Term->isUnconditional())
641 continue;
642
643 PhiInserter.Initialize(Boolean, "");
644 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), BoolFalse);
645
646 BasicBlock *Succ = Term->getSuccessor(0);
647 BBPredicates &Preds = (*FI == LoopEnd) ? LoopPred : Predicates[Succ];
648 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
649 PI != PE; ++PI) {
650
651 PhiInserter.AddAvailableValue(PI->first, PI->second);
652 }
653
654 Term->setCondition(PhiInserter.GetValueAtEndOfBlock(*FI));
655 }
656}
657
658/// Handle a rare case where the disintegrated nodes instructions
659/// no longer dominate all their uses. Not sure if this is really nessasary
660void AMDGPUStructurizeCFG::rebuildSSA() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000661 SSAUpdater Updater;
662 for (Region::block_iterator I = ParentRegion->block_begin(),
663 E = ParentRegion->block_end();
664 I != E; ++I) {
665
666 BasicBlock *BB = *I;
667 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
668 II != IE; ++II) {
669
670 bool Initialized = false;
671 for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {
672
673 Next = I->getNext();
674
675 Instruction *User = cast<Instruction>(I->getUser());
676 if (User->getParent() == BB) {
677 continue;
678
679 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
680 if (UserPN->getIncomingBlock(*I) == BB)
681 continue;
682 }
683
684 if (DT->dominates(II, User))
685 continue;
686
687 if (!Initialized) {
688 Value *Undef = UndefValue::get(II->getType());
689 Updater.Initialize(II->getType(), "");
690 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
691 Updater.AddAvailableValue(BB, II);
692 Initialized = true;
693 }
694 Updater.RewriteUseAfterInsertions(*I);
695 }
696 }
697 }
698}
699
700/// \brief Run the transformation for each region found
701bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000702 if (R->isTopLevelRegion())
703 return false;
704
705 Func = R->getEntry()->getParent();
706 ParentRegion = R;
707
708 DT = &getAnalysis<DominatorTree>();
709
710 orderNodes();
711 collectInfos();
712 createFlow();
713 insertConditions();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000714 setPhiValues();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000715 rebuildSSA();
716
717 Order.clear();
718 Visited.clear();
719 Predicates.clear();
720 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000721 AddedPhis.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000722 FlowsInserted.clear();
723
724 return true;
725}
726
727/// \brief Create the pass
728Pass *llvm::createAMDGPUStructurizeCFGPass() {
729 return new AMDGPUStructurizeCFG();
730}