blob: e97e049be58be2352e0e10acfa0c207a399a7672 [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;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000033
34typedef SmallVector<RegionNode*, 8> RNVector;
35typedef SmallVector<BasicBlock*, 8> BBVector;
Tom Stellard27f5d062013-02-08 22:24:37 +000036typedef SmallVector<BranchInst*, 8> BranchVector;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000037typedef SmallVector<BBValuePair, 2> BBValueVector;
38
Tom Stellard27f5d062013-02-08 22:24:37 +000039typedef SmallPtrSet<BasicBlock *, 8> BBSet;
40
Tom Stellard6b7d99d2012-12-19 22:10:31 +000041typedef DenseMap<PHINode *, BBValueVector> PhiMap;
Christian Konigf0e469b2013-02-16 11:27:29 +000042typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000043typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
44typedef DenseMap<BasicBlock *, Value *> BBPredicates;
45typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
Tom Stellard13cf6cb2013-02-08 22:24:35 +000046typedef DenseMap<BasicBlock *, BBVector> BB2BBVecMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000047
48// The name for newly created blocks.
49
50static const char *FlowBlockName = "Flow";
51
Christian Konigf0e469b2013-02-16 11:27:29 +000052/// @brief Find the nearest common dominator for multiple BasicBlocks
53///
54/// Helper class for AMDGPUStructurizeCFG
55/// TODO: Maybe move into common code
56class NearestCommonDominator {
57
58 DominatorTree *DT;
59
60 DTN2UnsignedMap IndexMap;
61
62 BasicBlock *Result;
63 unsigned ResultIndex;
64 bool ExplicitMentioned;
65
66public:
67 /// \brief Start a new query
68 NearestCommonDominator(DominatorTree *DomTree) {
69 DT = DomTree;
70 Result = 0;
71 }
72
73 /// \brief Add BB to the resulting dominator
74 void addBlock(BasicBlock *BB, bool Remember = true) {
75
76 DomTreeNode *Node = DT->getNode(BB);
77
78 if (Result == 0) {
79 unsigned Numbering = 0;
80 for (;Node;Node = Node->getIDom())
81 IndexMap[Node] = ++Numbering;
82 Result = BB;
83 ResultIndex = 1;
84 ExplicitMentioned = Remember;
85 return;
86 }
87
88 for (;Node;Node = Node->getIDom())
89 if (IndexMap.count(Node))
90 break;
91 else
92 IndexMap[Node] = 0;
93
94 assert(Node && "Dominator tree invalid!");
95
96 unsigned Numbering = IndexMap[Node];
97 if (Numbering > ResultIndex) {
98 Result = Node->getBlock();
99 ResultIndex = Numbering;
100 ExplicitMentioned = Remember && (Result == BB);
101 } else if (Numbering == ResultIndex) {
102 ExplicitMentioned |= Remember;
103 }
104 }
105
106 /// \brief Is "Result" one of the BBs added with "Remember" = True?
107 bool wasResultExplicitMentioned() {
108 return ExplicitMentioned;
109 }
110
111 /// \brief Get the query result
112 BasicBlock *getResult() {
113 return Result;
114 }
115};
116
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000117/// @brief Transforms the control flow graph on one single entry/exit region
118/// at a time.
119///
120/// After the transform all "If"/"Then"/"Else" style control flow looks like
121/// this:
122///
123/// \verbatim
124/// 1
125/// ||
126/// | |
127/// 2 |
128/// | /
129/// |/
130/// 3
131/// || Where:
132/// | | 1 = "If" block, calculates the condition
133/// 4 | 2 = "Then" subregion, runs if the condition is true
134/// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
135/// |/ 4 = "Else" optional subregion, runs if the condition is false
136/// 5 5 = "End" block, also rejoins the control flow
137/// \endverbatim
138///
139/// Control flow is expressed as a branch where the true exit goes into the
140/// "Then"/"Else" region, while the false exit skips the region
141/// The condition for the optional "Else" region is expressed as a PHI node.
142/// The incomming values of the PHI node are true for the "If" edge and false
143/// for the "Then" edge.
144///
145/// Additionally to that even complicated loops look like this:
146///
147/// \verbatim
148/// 1
149/// ||
150/// | |
151/// 2 ^ Where:
152/// | / 1 = "Entry" block
153/// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
154/// 3 3 = "Flow" block, with back edge to entry block
155/// |
156/// \endverbatim
157///
158/// The back edge of the "Flow" block is always on the false side of the branch
159/// while the true side continues the general flow. So the loop condition
160/// consist of a network of PHI nodes where the true incoming values expresses
161/// breaks and the false values expresses continue states.
162class AMDGPUStructurizeCFG : public RegionPass {
163
164 static char ID;
165
166 Type *Boolean;
167 ConstantInt *BoolTrue;
168 ConstantInt *BoolFalse;
169 UndefValue *BoolUndef;
170
171 Function *Func;
172 Region *ParentRegion;
173
174 DominatorTree *DT;
175
176 RNVector Order;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000177 BBSet Visited;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000178 PredMap Predicates;
179 BBPhiMap DeletedPhis;
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000180 BB2BBVecMap AddedPhis;
Tom Stellard27f5d062013-02-08 22:24:37 +0000181 BranchVector Conditions;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000182
183 BasicBlock *LoopStart;
184 BasicBlock *LoopEnd;
Tom Stellard27f5d062013-02-08 22:24:37 +0000185 BBSet LoopTargets;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000186 BBPredicates LoopPred;
187
188 void orderNodes();
189
Tom Stellard27f5d062013-02-08 22:24:37 +0000190 Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000191
Tom Stellard27f5d062013-02-08 22:24:37 +0000192 bool analyzeLoopStart(BasicBlock *From, BasicBlock *To, Value *Condition);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000193
Tom Stellard27f5d062013-02-08 22:24:37 +0000194 void analyzeNode(RegionNode *N);
195
196 void analyzeLoopEnd(RegionNode *N);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000197
198 void collectInfos();
199
Tom Stellard27f5d062013-02-08 22:24:37 +0000200 void insertConditions();
201
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000202 void delPhiValues(BasicBlock *From, BasicBlock *To);
203
204 void addPhiValues(BasicBlock *From, BasicBlock *To);
205
206 void setPhiValues();
207
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000208 void killTerminator(BasicBlock *BB);
209
Tom Stellardf4e471a2013-02-08 22:24:38 +0000210 void changeExit(RegionNode *Node, BasicBlock *NewExit,
211 bool IncludeDominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000212
Tom Stellardf4e471a2013-02-08 22:24:38 +0000213 BasicBlock *getNextFlow(BasicBlock *Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000214
Tom Stellardf4e471a2013-02-08 22:24:38 +0000215 BasicBlock *needPrefix(RegionNode *&Prev, RegionNode *Node);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000216
Tom Stellardf4e471a2013-02-08 22:24:38 +0000217 BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
218
219 RegionNode *getNextPrev(BasicBlock *Next);
220
221 bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
222
223 bool isPredictableTrue(RegionNode *Who, RegionNode *Where);
224
225 RegionNode *wireFlow(RegionNode *&Prev, bool ExitUseAllowed);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000226
227 void createFlow();
228
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000229 void rebuildSSA();
230
231public:
232 AMDGPUStructurizeCFG():
233 RegionPass(ID) {
234
235 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
236 }
237
238 virtual bool doInitialization(Region *R, RGPassManager &RGM);
239
240 virtual bool runOnRegion(Region *R, RGPassManager &RGM);
241
242 virtual const char *getPassName() const {
243 return "AMDGPU simplify control flow";
244 }
245
246 void getAnalysisUsage(AnalysisUsage &AU) const {
247
248 AU.addRequired<DominatorTree>();
249 AU.addPreserved<DominatorTree>();
250 RegionPass::getAnalysisUsage(AU);
251 }
252
253};
254
255} // end anonymous namespace
256
257char AMDGPUStructurizeCFG::ID = 0;
258
259/// \brief Initialize the types and constants used in the pass
260bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000261 LLVMContext &Context = R->getEntry()->getContext();
262
263 Boolean = Type::getInt1Ty(Context);
264 BoolTrue = ConstantInt::getTrue(Context);
265 BoolFalse = ConstantInt::getFalse(Context);
266 BoolUndef = UndefValue::get(Boolean);
267
268 return false;
269}
270
271/// \brief Build up the general order of nodes
272void AMDGPUStructurizeCFG::orderNodes() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000273 scc_iterator<Region *> I = scc_begin(ParentRegion),
274 E = scc_end(ParentRegion);
275 for (Order.clear(); I != E; ++I) {
276 std::vector<RegionNode *> &Nodes = *I;
277 Order.append(Nodes.begin(), Nodes.end());
278 }
279}
280
Tom Stellard27f5d062013-02-08 22:24:37 +0000281/// \brief Build the condition for one edge
282Value *AMDGPUStructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
283 bool Invert) {
284 Value *Cond = Invert ? BoolFalse : BoolTrue;
285 if (Term->isConditional()) {
286 Cond = Term->getCondition();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000287
Tom Stellard27f5d062013-02-08 22:24:37 +0000288 if (Idx != Invert)
289 Cond = BinaryOperator::CreateNot(Cond, "", Term);
290 }
291 return Cond;
292}
293
294/// \brief Analyze the start of a loop and insert predicates as necessary
295bool AMDGPUStructurizeCFG::analyzeLoopStart(BasicBlock *From, BasicBlock *To,
296 Value *Condition) {
297 LoopPred[From] = Condition;
298 LoopTargets.insert(To);
299 if (!LoopStart) {
300 LoopStart = To;
301 return true;
302
303 } else if (LoopStart == To)
304 return true;
305
306 // We need to handle the case of intersecting loops, e. g.
307 //
308 // /----<-----
309 // | |
310 // -> A -> B -> C -> D
311 // | |
312 // -----<----/
313
314 RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
315
316 for (;OI != OE; ++OI)
317 if ((*OI)->getEntry() == LoopStart)
318 break;
319
320 for (;OI != OE && (*OI)->getEntry() != To; ++OI) {
321 BBPredicates &Pred = Predicates[(*OI)->getEntry()];
322 if (!Pred.count(From))
323 Pred[From] = Condition;
324 }
325 return false;
326}
327
328/// \brief Analyze the predecessors of each block and build up predicates
329void AMDGPUStructurizeCFG::analyzeNode(RegionNode *N) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000330 RegionInfo *RI = ParentRegion->getRegionInfo();
Tom Stellard27f5d062013-02-08 22:24:37 +0000331 BasicBlock *BB = N->getEntry();
332 BBPredicates &Pred = Predicates[BB];
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000333
Tom Stellard27f5d062013-02-08 22:24:37 +0000334 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
335 PI != PE; ++PI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000336
Tom Stellard27f5d062013-02-08 22:24:37 +0000337 if (!ParentRegion->contains(*PI)) {
338 // It's a branch from outside into our region entry
339 Pred[*PI] = BoolTrue;
340 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000341 }
342
Tom Stellard27f5d062013-02-08 22:24:37 +0000343 Region *R = RI->getRegionFor(*PI);
344 if (R == ParentRegion) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000345
Tom Stellard27f5d062013-02-08 22:24:37 +0000346 // It's a top level block in our region
347 BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
348 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
349 BasicBlock *Succ = Term->getSuccessor(i);
350 if (Succ != BB)
351 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000352
Tom Stellard27f5d062013-02-08 22:24:37 +0000353 if (Visited.count(*PI)) {
354 // Normal forward edge
355 if (Term->isConditional()) {
356 // Try to treat it like an ELSE block
357 BasicBlock *Other = Term->getSuccessor(!i);
358 if (Visited.count(Other) && !LoopTargets.count(Other) &&
359 !Pred.count(Other) && !Pred.count(*PI)) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000360
Tom Stellard27f5d062013-02-08 22:24:37 +0000361 Pred[Other] = BoolFalse;
362 Pred[*PI] = BoolTrue;
363 continue;
364 }
365 }
366
367 } else {
368 // Back edge
369 if (analyzeLoopStart(*PI, BB, buildCondition(Term, i, true)))
370 continue;
371 }
372 Pred[*PI] = buildCondition(Term, i, false);
373 }
374
375 } else {
376
377 // It's an exit from a sub region
378 while(R->getParent() != ParentRegion)
379 R = R->getParent();
380
381 // Edge from inside a subregion to its entry, ignore it
382 if (R == N)
383 continue;
384
385 BasicBlock *Entry = R->getEntry();
386 if (!Visited.count(Entry))
387 if (analyzeLoopStart(Entry, BB, BoolFalse))
388 continue;
389
390 Pred[Entry] = BoolTrue;
391 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000392 }
393}
394
Tom Stellard27f5d062013-02-08 22:24:37 +0000395/// \brief Determine the end of the loop
396void AMDGPUStructurizeCFG::analyzeLoopEnd(RegionNode *N) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000397
Tom Stellard27f5d062013-02-08 22:24:37 +0000398 if (N->isSubRegion()) {
399 // Test for exit as back edge
400 BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
401 if (Visited.count(Exit))
402 LoopEnd = N->getEntry();
403
404 } else {
405 // Test for sucessors as back edge
406 BasicBlock *BB = N->getNodeAs<BasicBlock>();
407 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000408
409 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
410 BasicBlock *Succ = Term->getSuccessor(i);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000411
Tom Stellard27f5d062013-02-08 22:24:37 +0000412 if (Visited.count(Succ))
413 LoopEnd = BB;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000414 }
415 }
416}
417
418/// \brief Collect various loop and predicate infos
419void AMDGPUStructurizeCFG::collectInfos() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000420
421 // Reset predicate
422 Predicates.clear();
423
424 // and loop infos
425 LoopStart = LoopEnd = 0;
Tom Stellard27f5d062013-02-08 22:24:37 +0000426 LoopTargets.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000427 LoopPred.clear();
428
Tom Stellard27f5d062013-02-08 22:24:37 +0000429 // Reset the visited nodes
430 Visited.clear();
431
432 for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
433 OI != OE; ++OI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000434
435 // Analyze all the conditions leading to a node
Tom Stellard27f5d062013-02-08 22:24:37 +0000436 analyzeNode(*OI);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000437
Tom Stellard27f5d062013-02-08 22:24:37 +0000438 // Remember that we've seen this node
Tom Stellardf4e471a2013-02-08 22:24:38 +0000439 Visited.insert((*OI)->getEntry());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000440
Tom Stellard27f5d062013-02-08 22:24:37 +0000441 // Find the last back edge
442 analyzeLoopEnd(*OI);
443 }
444
445 // Both or neither must be set
446 assert(!LoopStart == !LoopEnd);
447}
448
449/// \brief Insert the missing branch conditions
450void AMDGPUStructurizeCFG::insertConditions() {
451 SSAUpdater PhiInserter;
452
453 for (BranchVector::iterator I = Conditions.begin(),
454 E = Conditions.end(); I != E; ++I) {
455
456 BranchInst *Term = *I;
457 BasicBlock *Parent = Term->getParent();
458
459 assert(Term->isConditional());
460
Christian Konig25bd8842013-02-16 11:27:40 +0000461 Value *Default = (Parent == LoopEnd) ? BoolTrue : BoolFalse;
Tom Stellard27f5d062013-02-08 22:24:37 +0000462
Christian Konig25bd8842013-02-16 11:27:40 +0000463 PhiInserter.Initialize(Boolean, "");
464 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
465 if (Parent == LoopEnd)
466 PhiInserter.AddAvailableValue(LoopStart, BoolTrue);
467 else
468 PhiInserter.AddAvailableValue(Parent, BoolFalse);
469
Tom Stellard27f5d062013-02-08 22:24:37 +0000470 BasicBlock *Succ = Term->getSuccessor(0);
471 BBPredicates &Preds = (Parent == LoopEnd) ? LoopPred : Predicates[Succ];
Christian Konig25bd8842013-02-16 11:27:40 +0000472
473 NearestCommonDominator Dominator(DT);
474 Dominator.addBlock(Parent, false);
475
476 Value *ParentValue = 0;
Tom Stellard27f5d062013-02-08 22:24:37 +0000477 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
478 PI != PE; ++PI) {
479
Christian Konig25bd8842013-02-16 11:27:40 +0000480 if (PI->first == Parent) {
481 ParentValue = PI->second;
482 break;
483 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000484 PhiInserter.AddAvailableValue(PI->first, PI->second);
Christian Konig25bd8842013-02-16 11:27:40 +0000485 Dominator.addBlock(PI->first);
Tom Stellard27f5d062013-02-08 22:24:37 +0000486 }
487
Christian Konig25bd8842013-02-16 11:27:40 +0000488 if (ParentValue) {
489 Term->setCondition(ParentValue);
490 } else {
491 if (!Dominator.wasResultExplicitMentioned())
492 PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
493
Tom Stellard27f5d062013-02-08 22:24:37 +0000494 Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
Christian Konig25bd8842013-02-16 11:27:40 +0000495 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000496 }
497}
498
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000499/// \brief Remove all PHI values coming from "From" into "To" and remember
500/// them in DeletedPhis
501void AMDGPUStructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
502 PhiMap &Map = DeletedPhis[To];
503 for (BasicBlock::iterator I = To->begin(), E = To->end();
504 I != E && isa<PHINode>(*I);) {
505
506 PHINode &Phi = cast<PHINode>(*I++);
507 while (Phi.getBasicBlockIndex(From) != -1) {
508 Value *Deleted = Phi.removeIncomingValue(From, false);
509 Map[&Phi].push_back(std::make_pair(From, Deleted));
510 }
511 }
512}
513
514/// \brief Add a dummy PHI value as soon as we knew the new predecessor
515void AMDGPUStructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
516 for (BasicBlock::iterator I = To->begin(), E = To->end();
517 I != E && isa<PHINode>(*I);) {
518
519 PHINode &Phi = cast<PHINode>(*I++);
520 Value *Undef = UndefValue::get(Phi.getType());
521 Phi.addIncoming(Undef, From);
522 }
523 AddedPhis[To].push_back(From);
524}
525
526/// \brief Add the real PHI value as soon as everything is set up
527void AMDGPUStructurizeCFG::setPhiValues() {
528
529 SSAUpdater Updater;
530 for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
531 AI != AE; ++AI) {
532
533 BasicBlock *To = AI->first;
534 BBVector &From = AI->second;
535
536 if (!DeletedPhis.count(To))
537 continue;
538
539 PhiMap &Map = DeletedPhis[To];
540 for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
541 PI != PE; ++PI) {
542
543 PHINode *Phi = PI->first;
544 Value *Undef = UndefValue::get(Phi->getType());
545 Updater.Initialize(Phi->getType(), "");
546 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
547 Updater.AddAvailableValue(To, Undef);
548
Christian Konig4c79c712013-02-16 11:27:35 +0000549 NearestCommonDominator Dominator(DT);
550 Dominator.addBlock(To, false);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000551 for (BBValueVector::iterator VI = PI->second.begin(),
552 VE = PI->second.end(); VI != VE; ++VI) {
553
554 Updater.AddAvailableValue(VI->first, VI->second);
Christian Konig4c79c712013-02-16 11:27:35 +0000555 Dominator.addBlock(VI->first);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000556 }
557
Christian Konig4c79c712013-02-16 11:27:35 +0000558 if (!Dominator.wasResultExplicitMentioned())
559 Updater.AddAvailableValue(Dominator.getResult(), Undef);
560
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000561 for (BBVector::iterator FI = From.begin(), FE = From.end();
562 FI != FE; ++FI) {
563
564 int Idx = Phi->getBasicBlockIndex(*FI);
565 assert(Idx != -1);
566 Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
567 }
568 }
569
570 DeletedPhis.erase(To);
571 }
572 assert(DeletedPhis.empty());
573}
574
Tom Stellardf4e471a2013-02-08 22:24:38 +0000575/// \brief Remove phi values from all successors and then remove the terminator.
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000576void AMDGPUStructurizeCFG::killTerminator(BasicBlock *BB) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000577 TerminatorInst *Term = BB->getTerminator();
578 if (!Term)
579 return;
580
581 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
582 SI != SE; ++SI) {
583
584 delPhiValues(BB, *SI);
585 }
586
587 Term->eraseFromParent();
588}
589
Tom Stellardf4e471a2013-02-08 22:24:38 +0000590/// \brief Let node exit(s) point to NewExit
591void AMDGPUStructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
592 bool IncludeDominator) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000593
Tom Stellardf4e471a2013-02-08 22:24:38 +0000594 if (Node->isSubRegion()) {
595 Region *SubRegion = Node->getNodeAs<Region>();
596 BasicBlock *OldExit = SubRegion->getExit();
597 BasicBlock *Dominator = 0;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000598
Tom Stellardf4e471a2013-02-08 22:24:38 +0000599 // Find all the edges from the sub region to the exit
600 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
601 I != E;) {
602
603 BasicBlock *BB = *I++;
604 if (!SubRegion->contains(BB))
605 continue;
606
607 // Modify the edges to point to the new exit
608 delPhiValues(BB, OldExit);
609 BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
610 addPhiValues(BB, NewExit);
611
612 // Find the new dominator (if requested)
613 if (IncludeDominator) {
614 if (!Dominator)
615 Dominator = BB;
616 else
617 Dominator = DT->findNearestCommonDominator(Dominator, BB);
618 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000619 }
620
Tom Stellardf4e471a2013-02-08 22:24:38 +0000621 // Change the dominator (if requested)
622 if (Dominator)
623 DT->changeImmediateDominator(NewExit, Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000624
Tom Stellardf4e471a2013-02-08 22:24:38 +0000625 // Update the region info
626 SubRegion->replaceExit(NewExit);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000627
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000628 } else {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000629 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
630 killTerminator(BB);
631 BranchInst::Create(NewExit, BB);
632 addPhiValues(BB, NewExit);
633 if (IncludeDominator)
634 DT->changeImmediateDominator(NewExit, BB);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000635 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000636}
637
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000638/// \brief Create a new flow node and update dominator tree and region info
Tom Stellardf4e471a2013-02-08 22:24:38 +0000639BasicBlock *AMDGPUStructurizeCFG::getNextFlow(BasicBlock *Dominator) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000640 LLVMContext &Context = Func->getContext();
641 BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
642 Order.back()->getEntry();
643 BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
644 Func, Insert);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000645 DT->addNewBlock(Flow, Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000646 ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000647 return Flow;
648}
649
Tom Stellardf4e471a2013-02-08 22:24:38 +0000650/// \brief Create a new or reuse the previous node as flow node
651BasicBlock *AMDGPUStructurizeCFG::needPrefix(RegionNode *&Prev,
652 RegionNode *Node) {
653
654 if (!Prev || Prev->isSubRegion() ||
655 (Node && Node->getEntry() == LoopStart)) {
656
657 // We need to insert a flow node, first figure out the dominator
658 DomTreeNode *Dominator = Prev ? DT->getNode(Prev->getEntry()) : 0;
659 if (!Dominator)
660 Dominator = DT->getNode(Node->getEntry())->getIDom();
661 assert(Dominator && "Illegal loop to function entry");
662
663 // then create the flow node
664 BasicBlock *Flow = getNextFlow(Dominator->getBlock());
665
666 // wire up the new flow
667 if (Prev) {
668 changeExit(Prev, Flow, true);
669 } else {
670 // Parent regions entry needs predicates, create a new region entry
671 BasicBlock *Entry = Node->getEntry();
672 for (pred_iterator I = pred_begin(Entry), E = pred_end(Entry);
673 I != E;) {
674
675 BasicBlock *BB = *(I++);
676 if (ParentRegion->contains(BB))
677 continue;
678
679 // Remove PHY values from outside to our entry node
680 delPhiValues(BB, Entry);
681
682 // Update the branch instructions
683 BB->getTerminator()->replaceUsesOfWith(Entry, Flow);
684 }
685
686 // Populate the region tree with the new entry
687 for (Region *R = ParentRegion; R && R->getEntry() == Entry;
688 R = R->getParent()) {
689 R->replaceEntry(Flow);
690 }
691 }
692 Prev = ParentRegion->getBBNode(Flow);
693
694 } else {
695 killTerminator(Prev->getEntry());
696 }
697
698 return Prev->getEntry();
699}
700
701/// \brief Returns the region exit if possible, otherwise just a new flow node
702BasicBlock *AMDGPUStructurizeCFG::needPostfix(BasicBlock *Flow,
703 bool ExitUseAllowed) {
704
705 if (Order.empty() && ExitUseAllowed) {
706 BasicBlock *Exit = ParentRegion->getExit();
707 DT->changeImmediateDominator(Exit, Flow);
708 addPhiValues(Flow, Exit);
709 return Exit;
710 }
711 return getNextFlow(Flow);
712}
713
714/// \brief Returns the region node for Netx, or null if Next is the exit
715RegionNode *AMDGPUStructurizeCFG::getNextPrev(BasicBlock *Next) {
716 return ParentRegion->contains(Next) ? ParentRegion->getBBNode(Next) : 0;
717}
718
719/// \brief Does BB dominate all the predicates of Node ?
720bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
721 BBPredicates &Preds = Predicates[Node->getEntry()];
722 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
723 PI != PE; ++PI) {
724
725 if (!DT->dominates(BB, PI->first))
726 return false;
727 }
728 return true;
729}
730
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000731/// \brief Can we predict that this node will always be called?
Tom Stellardf4e471a2013-02-08 22:24:38 +0000732bool AMDGPUStructurizeCFG::isPredictableTrue(RegionNode *Who,
733 RegionNode *Where) {
734
735 BBPredicates &Preds = Predicates[Who->getEntry()];
736 bool Dominated = Where == 0;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000737
738 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
739 I != E; ++I) {
740
741 if (I->second != BoolTrue)
742 return false;
743
Tom Stellardf4e471a2013-02-08 22:24:38 +0000744 if (!Dominated && DT->dominates(I->first, Where->getEntry()))
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000745 Dominated = true;
746 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000747
748 // TODO: The dominator check is too strict
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000749 return Dominated;
750}
751
Tom Stellardf4e471a2013-02-08 22:24:38 +0000752/// Take one node from the order vector and wire it up
753RegionNode *AMDGPUStructurizeCFG::wireFlow(RegionNode *&Prev,
754 bool ExitUseAllowed) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000755
Tom Stellardf4e471a2013-02-08 22:24:38 +0000756 RegionNode *Node = Order.pop_back_val();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000757
Tom Stellardf4e471a2013-02-08 22:24:38 +0000758 if (isPredictableTrue(Node, Prev)) {
759 // Just a linear flow
760 if (Prev) {
761 changeExit(Prev, Node->getEntry(), true);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000762 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000763 Prev = Node;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000764
765 } else {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000766 // Insert extra prefix node (or reuse last one)
767 BasicBlock *Flow = needPrefix(Prev, Node);
768 if (Node->getEntry() == LoopStart)
769 LoopStart = Flow;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000770
Tom Stellardf4e471a2013-02-08 22:24:38 +0000771 // Insert extra postfix node (or use exit instead)
772 BasicBlock *Entry = Node->getEntry();
773 BasicBlock *Next = needPostfix(Flow, ExitUseAllowed && Entry != LoopEnd);
774
775 // let it point to entry and next block
776 Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
777 addPhiValues(Flow, Entry);
778 DT->changeImmediateDominator(Entry, Flow);
779
780 Prev = Node;
781 while (!Order.empty() && Node->getEntry() != LoopEnd &&
782 !LoopTargets.count(Order.back()->getEntry()) &&
783 dominatesPredicates(Entry, Order.back())) {
784 Node = wireFlow(Prev, false);
785 }
786
787 changeExit(Prev, Next, false);
788 Prev = getNextPrev(Next);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000789 }
790
Tom Stellardf4e471a2013-02-08 22:24:38 +0000791 return Node;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000792}
793
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000794/// After this function control flow looks like it should be, but
Tom Stellardf4e471a2013-02-08 22:24:38 +0000795/// branches and PHI nodes only have undefined conditions.
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000796void AMDGPUStructurizeCFG::createFlow() {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000797
798 BasicBlock *Exit = ParentRegion->getExit();
799 bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
800
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000801 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000802 AddedPhis.clear();
Tom Stellardf4e471a2013-02-08 22:24:38 +0000803 Conditions.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000804
Tom Stellardf4e471a2013-02-08 22:24:38 +0000805 RegionNode *Prev = 0;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000806 while (!Order.empty()) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000807
808 RegionNode *Node = wireFlow(Prev, EntryDominatesExit);
809
810 // Create an extra loop end node
811 if (Node->getEntry() == LoopEnd) {
812 LoopEnd = needPrefix(Prev, 0);
813 BasicBlock *Next = needPostfix(LoopEnd, EntryDominatesExit);
814
815 Conditions.push_back(BranchInst::Create(Next, LoopStart,
Tom Stellard27f5d062013-02-08 22:24:37 +0000816 BoolUndef, LoopEnd));
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000817 addPhiValues(LoopEnd, LoopStart);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000818 Prev = getNextPrev(Next);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000819 }
820 }
821
Tom Stellardf4e471a2013-02-08 22:24:38 +0000822 if (Prev)
823 changeExit(Prev, Exit, EntryDominatesExit);
824 else
825 assert(EntryDominatesExit);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000826}
827
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000828/// Handle a rare case where the disintegrated nodes instructions
829/// no longer dominate all their uses. Not sure if this is really nessasary
830void AMDGPUStructurizeCFG::rebuildSSA() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000831 SSAUpdater Updater;
832 for (Region::block_iterator I = ParentRegion->block_begin(),
833 E = ParentRegion->block_end();
834 I != E; ++I) {
835
836 BasicBlock *BB = *I;
837 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
838 II != IE; ++II) {
839
840 bool Initialized = false;
841 for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {
842
843 Next = I->getNext();
844
845 Instruction *User = cast<Instruction>(I->getUser());
846 if (User->getParent() == BB) {
847 continue;
848
849 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
850 if (UserPN->getIncomingBlock(*I) == BB)
851 continue;
852 }
853
854 if (DT->dominates(II, User))
855 continue;
856
857 if (!Initialized) {
858 Value *Undef = UndefValue::get(II->getType());
859 Updater.Initialize(II->getType(), "");
860 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
861 Updater.AddAvailableValue(BB, II);
862 Initialized = true;
863 }
864 Updater.RewriteUseAfterInsertions(*I);
865 }
866 }
867 }
868}
869
870/// \brief Run the transformation for each region found
871bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000872 if (R->isTopLevelRegion())
873 return false;
874
875 Func = R->getEntry()->getParent();
876 ParentRegion = R;
877
878 DT = &getAnalysis<DominatorTree>();
879
880 orderNodes();
881 collectInfos();
882 createFlow();
883 insertConditions();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000884 setPhiValues();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000885 rebuildSSA();
886
Tom Stellard27f5d062013-02-08 22:24:37 +0000887 // Cleanup
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000888 Order.clear();
889 Visited.clear();
890 Predicates.clear();
891 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000892 AddedPhis.clear();
Tom Stellard27f5d062013-02-08 22:24:37 +0000893 Conditions.clear();
894 LoopTargets.clear();
895 LoopPred.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000896
897 return true;
898}
899
900/// \brief Create the pass
901Pass *llvm::createAMDGPUStructurizeCFGPass() {
902 return new AMDGPUStructurizeCFG();
903}