blob: c2b084afc7ec21c8f3e1a473397ccae13fb634cd [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;
Christian Konig623977d2013-02-16 11:27:45 +000046typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
Tom Stellard13cf6cb2013-02-08 22:24:35 +000047typedef DenseMap<BasicBlock *, BBVector> BB2BBVecMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000048
49// The name for newly created blocks.
50
51static const char *FlowBlockName = "Flow";
52
Christian Konigf0e469b2013-02-16 11:27:29 +000053/// @brief Find the nearest common dominator for multiple BasicBlocks
54///
55/// Helper class for AMDGPUStructurizeCFG
56/// TODO: Maybe move into common code
57class NearestCommonDominator {
58
59 DominatorTree *DT;
60
61 DTN2UnsignedMap IndexMap;
62
63 BasicBlock *Result;
64 unsigned ResultIndex;
65 bool ExplicitMentioned;
66
67public:
68 /// \brief Start a new query
69 NearestCommonDominator(DominatorTree *DomTree) {
70 DT = DomTree;
71 Result = 0;
72 }
73
74 /// \brief Add BB to the resulting dominator
75 void addBlock(BasicBlock *BB, bool Remember = true) {
76
77 DomTreeNode *Node = DT->getNode(BB);
78
79 if (Result == 0) {
80 unsigned Numbering = 0;
81 for (;Node;Node = Node->getIDom())
82 IndexMap[Node] = ++Numbering;
83 Result = BB;
84 ResultIndex = 1;
85 ExplicitMentioned = Remember;
86 return;
87 }
88
89 for (;Node;Node = Node->getIDom())
90 if (IndexMap.count(Node))
91 break;
92 else
93 IndexMap[Node] = 0;
94
95 assert(Node && "Dominator tree invalid!");
96
97 unsigned Numbering = IndexMap[Node];
98 if (Numbering > ResultIndex) {
99 Result = Node->getBlock();
100 ResultIndex = Numbering;
101 ExplicitMentioned = Remember && (Result == BB);
102 } else if (Numbering == ResultIndex) {
103 ExplicitMentioned |= Remember;
104 }
105 }
106
107 /// \brief Is "Result" one of the BBs added with "Remember" = True?
108 bool wasResultExplicitMentioned() {
109 return ExplicitMentioned;
110 }
111
112 /// \brief Get the query result
113 BasicBlock *getResult() {
114 return Result;
115 }
116};
117
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000118/// @brief Transforms the control flow graph on one single entry/exit region
119/// at a time.
120///
121/// After the transform all "If"/"Then"/"Else" style control flow looks like
122/// this:
123///
124/// \verbatim
125/// 1
126/// ||
127/// | |
128/// 2 |
129/// | /
130/// |/
131/// 3
132/// || Where:
133/// | | 1 = "If" block, calculates the condition
134/// 4 | 2 = "Then" subregion, runs if the condition is true
135/// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
136/// |/ 4 = "Else" optional subregion, runs if the condition is false
137/// 5 5 = "End" block, also rejoins the control flow
138/// \endverbatim
139///
140/// Control flow is expressed as a branch where the true exit goes into the
141/// "Then"/"Else" region, while the false exit skips the region
142/// The condition for the optional "Else" region is expressed as a PHI node.
143/// The incomming values of the PHI node are true for the "If" edge and false
144/// for the "Then" edge.
145///
146/// Additionally to that even complicated loops look like this:
147///
148/// \verbatim
149/// 1
150/// ||
151/// | |
152/// 2 ^ Where:
153/// | / 1 = "Entry" block
154/// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
155/// 3 3 = "Flow" block, with back edge to entry block
156/// |
157/// \endverbatim
158///
159/// The back edge of the "Flow" block is always on the false side of the branch
160/// while the true side continues the general flow. So the loop condition
161/// consist of a network of PHI nodes where the true incoming values expresses
162/// breaks and the false values expresses continue states.
163class AMDGPUStructurizeCFG : public RegionPass {
164
165 static char ID;
166
167 Type *Boolean;
168 ConstantInt *BoolTrue;
169 ConstantInt *BoolFalse;
170 UndefValue *BoolUndef;
171
172 Function *Func;
173 Region *ParentRegion;
174
175 DominatorTree *DT;
176
177 RNVector Order;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000178 BBSet Visited;
Christian Konig623977d2013-02-16 11:27:45 +0000179
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000180 BBPhiMap DeletedPhis;
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000181 BB2BBVecMap AddedPhis;
Christian Konig623977d2013-02-16 11:27:45 +0000182
183 PredMap Predicates;
Tom Stellard27f5d062013-02-08 22:24:37 +0000184 BranchVector Conditions;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000185
Christian Konig623977d2013-02-16 11:27:45 +0000186 BB2BBMap Loops;
187 PredMap LoopPreds;
188 BranchVector LoopConds;
189
190 RegionNode *PrevNode;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000191
192 void orderNodes();
193
Christian Konig623977d2013-02-16 11:27:45 +0000194 void analyzeLoops(RegionNode *N);
195
Tom Stellard27f5d062013-02-08 22:24:37 +0000196 Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000197
Christian Konig623977d2013-02-16 11:27:45 +0000198 void gatherPredicates(RegionNode *N);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000199
200 void collectInfos();
201
Christian Konig623977d2013-02-16 11:27:45 +0000202 void insertConditions(bool Loops);
Tom Stellard27f5d062013-02-08 22:24:37 +0000203
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000204 void delPhiValues(BasicBlock *From, BasicBlock *To);
205
206 void addPhiValues(BasicBlock *From, BasicBlock *To);
207
208 void setPhiValues();
209
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000210 void killTerminator(BasicBlock *BB);
211
Tom Stellardf4e471a2013-02-08 22:24:38 +0000212 void changeExit(RegionNode *Node, BasicBlock *NewExit,
213 bool IncludeDominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000214
Tom Stellardf4e471a2013-02-08 22:24:38 +0000215 BasicBlock *getNextFlow(BasicBlock *Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000216
Christian Konig623977d2013-02-16 11:27:45 +0000217 BasicBlock *needPrefix(bool NeedEmpty);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000218
Tom Stellardf4e471a2013-02-08 22:24:38 +0000219 BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
220
Christian Konig623977d2013-02-16 11:27:45 +0000221 void setPrevNode(BasicBlock *BB);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000222
223 bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
224
Christian Konig623977d2013-02-16 11:27:45 +0000225 bool isPredictableTrue(RegionNode *Node);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000226
Christian Konig623977d2013-02-16 11:27:45 +0000227 void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
228
229 void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000230
231 void createFlow();
232
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000233 void rebuildSSA();
234
235public:
236 AMDGPUStructurizeCFG():
237 RegionPass(ID) {
238
239 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
240 }
241
242 virtual bool doInitialization(Region *R, RGPassManager &RGM);
243
244 virtual bool runOnRegion(Region *R, RGPassManager &RGM);
245
246 virtual const char *getPassName() const {
247 return "AMDGPU simplify control flow";
248 }
249
250 void getAnalysisUsage(AnalysisUsage &AU) const {
251
252 AU.addRequired<DominatorTree>();
253 AU.addPreserved<DominatorTree>();
254 RegionPass::getAnalysisUsage(AU);
255 }
256
257};
258
259} // end anonymous namespace
260
261char AMDGPUStructurizeCFG::ID = 0;
262
263/// \brief Initialize the types and constants used in the pass
264bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000265 LLVMContext &Context = R->getEntry()->getContext();
266
267 Boolean = Type::getInt1Ty(Context);
268 BoolTrue = ConstantInt::getTrue(Context);
269 BoolFalse = ConstantInt::getFalse(Context);
270 BoolUndef = UndefValue::get(Boolean);
271
272 return false;
273}
274
275/// \brief Build up the general order of nodes
276void AMDGPUStructurizeCFG::orderNodes() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000277 scc_iterator<Region *> I = scc_begin(ParentRegion),
278 E = scc_end(ParentRegion);
279 for (Order.clear(); I != E; ++I) {
280 std::vector<RegionNode *> &Nodes = *I;
281 Order.append(Nodes.begin(), Nodes.end());
282 }
283}
284
Christian Konig623977d2013-02-16 11:27:45 +0000285/// \brief Determine the end of the loops
286void AMDGPUStructurizeCFG::analyzeLoops(RegionNode *N) {
287
288 if (N->isSubRegion()) {
289 // Test for exit as back edge
290 BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
291 if (Visited.count(Exit))
292 Loops[Exit] = N->getEntry();
293
294 } else {
295 // Test for sucessors as back edge
296 BasicBlock *BB = N->getNodeAs<BasicBlock>();
297 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
298
299 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
300 BasicBlock *Succ = Term->getSuccessor(i);
301
302 if (Visited.count(Succ))
303 Loops[Succ] = BB;
304 }
305 }
306}
307
Tom Stellard27f5d062013-02-08 22:24:37 +0000308/// \brief Build the condition for one edge
309Value *AMDGPUStructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
310 bool Invert) {
311 Value *Cond = Invert ? BoolFalse : BoolTrue;
312 if (Term->isConditional()) {
313 Cond = Term->getCondition();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000314
Tom Stellard27f5d062013-02-08 22:24:37 +0000315 if (Idx != Invert)
316 Cond = BinaryOperator::CreateNot(Cond, "", Term);
317 }
318 return Cond;
319}
320
Tom Stellard27f5d062013-02-08 22:24:37 +0000321/// \brief Analyze the predecessors of each block and build up predicates
Christian Konig623977d2013-02-16 11:27:45 +0000322void AMDGPUStructurizeCFG::gatherPredicates(RegionNode *N) {
323
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000324 RegionInfo *RI = ParentRegion->getRegionInfo();
Tom Stellard27f5d062013-02-08 22:24:37 +0000325 BasicBlock *BB = N->getEntry();
326 BBPredicates &Pred = Predicates[BB];
Christian Konig623977d2013-02-16 11:27:45 +0000327 BBPredicates &LPred = LoopPreds[BB];
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000328
Tom Stellard27f5d062013-02-08 22:24:37 +0000329 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
330 PI != PE; ++PI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000331
Christian Konig623977d2013-02-16 11:27:45 +0000332 // Ignore it if it's a branch from outside into our region entry
333 if (!ParentRegion->contains(*PI))
Tom Stellard27f5d062013-02-08 22:24:37 +0000334 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000335
Tom Stellard27f5d062013-02-08 22:24:37 +0000336 Region *R = RI->getRegionFor(*PI);
337 if (R == ParentRegion) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000338
Tom Stellard27f5d062013-02-08 22:24:37 +0000339 // It's a top level block in our region
340 BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
341 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
342 BasicBlock *Succ = Term->getSuccessor(i);
343 if (Succ != BB)
344 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000345
Tom Stellard27f5d062013-02-08 22:24:37 +0000346 if (Visited.count(*PI)) {
347 // Normal forward edge
348 if (Term->isConditional()) {
349 // Try to treat it like an ELSE block
350 BasicBlock *Other = Term->getSuccessor(!i);
Christian Konig623977d2013-02-16 11:27:45 +0000351 if (Visited.count(Other) && !Loops.count(Other) &&
Tom Stellard27f5d062013-02-08 22:24:37 +0000352 !Pred.count(Other) && !Pred.count(*PI)) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000353
Tom Stellard27f5d062013-02-08 22:24:37 +0000354 Pred[Other] = BoolFalse;
355 Pred[*PI] = BoolTrue;
356 continue;
357 }
358 }
Christian Konig623977d2013-02-16 11:27:45 +0000359 Pred[*PI] = buildCondition(Term, i, false);
360
Tom Stellard27f5d062013-02-08 22:24:37 +0000361 } else {
362 // Back edge
Christian Konig623977d2013-02-16 11:27:45 +0000363 LPred[*PI] = buildCondition(Term, i, true);
Tom Stellard27f5d062013-02-08 22:24:37 +0000364 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000365 }
366
367 } else {
368
369 // It's an exit from a sub region
370 while(R->getParent() != ParentRegion)
371 R = R->getParent();
372
373 // Edge from inside a subregion to its entry, ignore it
374 if (R == N)
375 continue;
376
377 BasicBlock *Entry = R->getEntry();
Christian Konig623977d2013-02-16 11:27:45 +0000378 if (Visited.count(Entry))
379 Pred[Entry] = BoolTrue;
380 else
381 LPred[Entry] = BoolFalse;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000382 }
383 }
384}
385
386/// \brief Collect various loop and predicate infos
387void AMDGPUStructurizeCFG::collectInfos() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000388
389 // Reset predicate
390 Predicates.clear();
391
392 // and loop infos
Christian Konig623977d2013-02-16 11:27:45 +0000393 Loops.clear();
394 LoopPreds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000395
Tom Stellard27f5d062013-02-08 22:24:37 +0000396 // Reset the visited nodes
397 Visited.clear();
398
399 for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
400 OI != OE; ++OI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000401
402 // Analyze all the conditions leading to a node
Christian Konig623977d2013-02-16 11:27:45 +0000403 gatherPredicates(*OI);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000404
Tom Stellard27f5d062013-02-08 22:24:37 +0000405 // Remember that we've seen this node
Tom Stellardf4e471a2013-02-08 22:24:38 +0000406 Visited.insert((*OI)->getEntry());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000407
Christian Konig623977d2013-02-16 11:27:45 +0000408 // Find the last back edges
409 analyzeLoops(*OI);
Tom Stellard27f5d062013-02-08 22:24:37 +0000410 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000411}
412
413/// \brief Insert the missing branch conditions
Christian Konig623977d2013-02-16 11:27:45 +0000414void AMDGPUStructurizeCFG::insertConditions(bool Loops) {
415 BranchVector &Conds = Loops ? LoopConds : Conditions;
416 Value *Default = Loops ? BoolTrue : BoolFalse;
Tom Stellard27f5d062013-02-08 22:24:37 +0000417 SSAUpdater PhiInserter;
418
Christian Konig623977d2013-02-16 11:27:45 +0000419 for (BranchVector::iterator I = Conds.begin(),
420 E = Conds.end(); I != E; ++I) {
Tom Stellard27f5d062013-02-08 22:24:37 +0000421
422 BranchInst *Term = *I;
Tom Stellard27f5d062013-02-08 22:24:37 +0000423 assert(Term->isConditional());
424
Christian Konig623977d2013-02-16 11:27:45 +0000425 BasicBlock *Parent = Term->getParent();
426 BasicBlock *SuccTrue = Term->getSuccessor(0);
427 BasicBlock *SuccFalse = Term->getSuccessor(1);
Tom Stellard27f5d062013-02-08 22:24:37 +0000428
Christian Konig25bd8842013-02-16 11:27:40 +0000429 PhiInserter.Initialize(Boolean, "");
430 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
Christian Konig623977d2013-02-16 11:27:45 +0000431 PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
Christian Konig25bd8842013-02-16 11:27:40 +0000432
Christian Konig623977d2013-02-16 11:27:45 +0000433 BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
Christian Konig25bd8842013-02-16 11:27:40 +0000434
435 NearestCommonDominator Dominator(DT);
436 Dominator.addBlock(Parent, false);
437
438 Value *ParentValue = 0;
Tom Stellard27f5d062013-02-08 22:24:37 +0000439 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
440 PI != PE; ++PI) {
441
Christian Konig25bd8842013-02-16 11:27:40 +0000442 if (PI->first == Parent) {
443 ParentValue = PI->second;
444 break;
445 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000446 PhiInserter.AddAvailableValue(PI->first, PI->second);
Christian Konig25bd8842013-02-16 11:27:40 +0000447 Dominator.addBlock(PI->first);
Tom Stellard27f5d062013-02-08 22:24:37 +0000448 }
449
Christian Konig25bd8842013-02-16 11:27:40 +0000450 if (ParentValue) {
451 Term->setCondition(ParentValue);
452 } else {
453 if (!Dominator.wasResultExplicitMentioned())
454 PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
455
Tom Stellard27f5d062013-02-08 22:24:37 +0000456 Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
Christian Konig25bd8842013-02-16 11:27:40 +0000457 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000458 }
459}
460
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000461/// \brief Remove all PHI values coming from "From" into "To" and remember
462/// them in DeletedPhis
463void AMDGPUStructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
464 PhiMap &Map = DeletedPhis[To];
465 for (BasicBlock::iterator I = To->begin(), E = To->end();
466 I != E && isa<PHINode>(*I);) {
467
468 PHINode &Phi = cast<PHINode>(*I++);
469 while (Phi.getBasicBlockIndex(From) != -1) {
470 Value *Deleted = Phi.removeIncomingValue(From, false);
471 Map[&Phi].push_back(std::make_pair(From, Deleted));
472 }
473 }
474}
475
476/// \brief Add a dummy PHI value as soon as we knew the new predecessor
477void AMDGPUStructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
478 for (BasicBlock::iterator I = To->begin(), E = To->end();
479 I != E && isa<PHINode>(*I);) {
480
481 PHINode &Phi = cast<PHINode>(*I++);
482 Value *Undef = UndefValue::get(Phi.getType());
483 Phi.addIncoming(Undef, From);
484 }
485 AddedPhis[To].push_back(From);
486}
487
488/// \brief Add the real PHI value as soon as everything is set up
489void AMDGPUStructurizeCFG::setPhiValues() {
490
491 SSAUpdater Updater;
492 for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
493 AI != AE; ++AI) {
494
495 BasicBlock *To = AI->first;
496 BBVector &From = AI->second;
497
498 if (!DeletedPhis.count(To))
499 continue;
500
501 PhiMap &Map = DeletedPhis[To];
502 for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
503 PI != PE; ++PI) {
504
505 PHINode *Phi = PI->first;
506 Value *Undef = UndefValue::get(Phi->getType());
507 Updater.Initialize(Phi->getType(), "");
508 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
509 Updater.AddAvailableValue(To, Undef);
510
Christian Konig4c79c712013-02-16 11:27:35 +0000511 NearestCommonDominator Dominator(DT);
512 Dominator.addBlock(To, false);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000513 for (BBValueVector::iterator VI = PI->second.begin(),
514 VE = PI->second.end(); VI != VE; ++VI) {
515
516 Updater.AddAvailableValue(VI->first, VI->second);
Christian Konig4c79c712013-02-16 11:27:35 +0000517 Dominator.addBlock(VI->first);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000518 }
519
Christian Konig4c79c712013-02-16 11:27:35 +0000520 if (!Dominator.wasResultExplicitMentioned())
521 Updater.AddAvailableValue(Dominator.getResult(), Undef);
522
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000523 for (BBVector::iterator FI = From.begin(), FE = From.end();
524 FI != FE; ++FI) {
525
526 int Idx = Phi->getBasicBlockIndex(*FI);
527 assert(Idx != -1);
528 Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
529 }
530 }
531
532 DeletedPhis.erase(To);
533 }
534 assert(DeletedPhis.empty());
535}
536
Tom Stellardf4e471a2013-02-08 22:24:38 +0000537/// \brief Remove phi values from all successors and then remove the terminator.
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000538void AMDGPUStructurizeCFG::killTerminator(BasicBlock *BB) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000539 TerminatorInst *Term = BB->getTerminator();
540 if (!Term)
541 return;
542
543 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
544 SI != SE; ++SI) {
545
546 delPhiValues(BB, *SI);
547 }
548
549 Term->eraseFromParent();
550}
551
Tom Stellardf4e471a2013-02-08 22:24:38 +0000552/// \brief Let node exit(s) point to NewExit
553void AMDGPUStructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
554 bool IncludeDominator) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000555
Tom Stellardf4e471a2013-02-08 22:24:38 +0000556 if (Node->isSubRegion()) {
557 Region *SubRegion = Node->getNodeAs<Region>();
558 BasicBlock *OldExit = SubRegion->getExit();
559 BasicBlock *Dominator = 0;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000560
Tom Stellardf4e471a2013-02-08 22:24:38 +0000561 // Find all the edges from the sub region to the exit
562 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
563 I != E;) {
564
565 BasicBlock *BB = *I++;
566 if (!SubRegion->contains(BB))
567 continue;
568
569 // Modify the edges to point to the new exit
570 delPhiValues(BB, OldExit);
571 BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
572 addPhiValues(BB, NewExit);
573
574 // Find the new dominator (if requested)
575 if (IncludeDominator) {
576 if (!Dominator)
577 Dominator = BB;
578 else
579 Dominator = DT->findNearestCommonDominator(Dominator, BB);
580 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000581 }
582
Tom Stellardf4e471a2013-02-08 22:24:38 +0000583 // Change the dominator (if requested)
584 if (Dominator)
585 DT->changeImmediateDominator(NewExit, Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000586
Tom Stellardf4e471a2013-02-08 22:24:38 +0000587 // Update the region info
588 SubRegion->replaceExit(NewExit);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000589
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000590 } else {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000591 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
592 killTerminator(BB);
593 BranchInst::Create(NewExit, BB);
594 addPhiValues(BB, NewExit);
595 if (IncludeDominator)
596 DT->changeImmediateDominator(NewExit, BB);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000597 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000598}
599
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000600/// \brief Create a new flow node and update dominator tree and region info
Tom Stellardf4e471a2013-02-08 22:24:38 +0000601BasicBlock *AMDGPUStructurizeCFG::getNextFlow(BasicBlock *Dominator) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000602 LLVMContext &Context = Func->getContext();
603 BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
604 Order.back()->getEntry();
605 BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
606 Func, Insert);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000607 DT->addNewBlock(Flow, Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000608 ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000609 return Flow;
610}
611
Tom Stellardf4e471a2013-02-08 22:24:38 +0000612/// \brief Create a new or reuse the previous node as flow node
Christian Konig623977d2013-02-16 11:27:45 +0000613BasicBlock *AMDGPUStructurizeCFG::needPrefix(bool NeedEmpty) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000614
Christian Konig623977d2013-02-16 11:27:45 +0000615 BasicBlock *Entry = PrevNode->getEntry();
Tom Stellardf4e471a2013-02-08 22:24:38 +0000616
Christian Konig623977d2013-02-16 11:27:45 +0000617 if (!PrevNode->isSubRegion()) {
618 killTerminator(Entry);
619 if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
620 return Entry;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000621
Christian Konig623977d2013-02-16 11:27:45 +0000622 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000623
Christian Konig623977d2013-02-16 11:27:45 +0000624 // create a new flow node
625 BasicBlock *Flow = getNextFlow(Entry);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000626
Christian Konig623977d2013-02-16 11:27:45 +0000627 // and wire it up
628 changeExit(PrevNode, Flow, true);
629 PrevNode = ParentRegion->getBBNode(Flow);
630 return Flow;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000631}
632
633/// \brief Returns the region exit if possible, otherwise just a new flow node
634BasicBlock *AMDGPUStructurizeCFG::needPostfix(BasicBlock *Flow,
635 bool ExitUseAllowed) {
636
637 if (Order.empty() && ExitUseAllowed) {
638 BasicBlock *Exit = ParentRegion->getExit();
639 DT->changeImmediateDominator(Exit, Flow);
640 addPhiValues(Flow, Exit);
641 return Exit;
642 }
643 return getNextFlow(Flow);
644}
645
Christian Konig623977d2013-02-16 11:27:45 +0000646/// \brief Set the previous node
647void AMDGPUStructurizeCFG::setPrevNode(BasicBlock *BB) {
648 PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) : 0;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000649}
650
651/// \brief Does BB dominate all the predicates of Node ?
652bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
653 BBPredicates &Preds = Predicates[Node->getEntry()];
654 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
655 PI != PE; ++PI) {
656
657 if (!DT->dominates(BB, PI->first))
658 return false;
659 }
660 return true;
661}
662
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000663/// \brief Can we predict that this node will always be called?
Christian Konig623977d2013-02-16 11:27:45 +0000664bool AMDGPUStructurizeCFG::isPredictableTrue(RegionNode *Node) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000665
Christian Konig623977d2013-02-16 11:27:45 +0000666 BBPredicates &Preds = Predicates[Node->getEntry()];
667 bool Dominated = false;
668
669 // Regionentry is always true
670 if (PrevNode == 0)
671 return true;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000672
673 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
674 I != E; ++I) {
675
676 if (I->second != BoolTrue)
677 return false;
678
Christian Konig623977d2013-02-16 11:27:45 +0000679 if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000680 Dominated = true;
681 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000682
683 // TODO: The dominator check is too strict
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000684 return Dominated;
685}
686
Tom Stellardf4e471a2013-02-08 22:24:38 +0000687/// Take one node from the order vector and wire it up
Christian Konig623977d2013-02-16 11:27:45 +0000688void AMDGPUStructurizeCFG::wireFlow(bool ExitUseAllowed,
689 BasicBlock *LoopEnd) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000690
Tom Stellardf4e471a2013-02-08 22:24:38 +0000691 RegionNode *Node = Order.pop_back_val();
Christian Konig623977d2013-02-16 11:27:45 +0000692 Visited.insert(Node->getEntry());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000693
Christian Konig623977d2013-02-16 11:27:45 +0000694 if (isPredictableTrue(Node)) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000695 // Just a linear flow
Christian Konig623977d2013-02-16 11:27:45 +0000696 if (PrevNode) {
697 changeExit(PrevNode, Node->getEntry(), true);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000698 }
Christian Konig623977d2013-02-16 11:27:45 +0000699 PrevNode = Node;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000700
701 } else {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000702 // Insert extra prefix node (or reuse last one)
Christian Konig623977d2013-02-16 11:27:45 +0000703 BasicBlock *Flow = needPrefix(false);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000704
Tom Stellardf4e471a2013-02-08 22:24:38 +0000705 // Insert extra postfix node (or use exit instead)
706 BasicBlock *Entry = Node->getEntry();
Christian Konig623977d2013-02-16 11:27:45 +0000707 BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000708
709 // let it point to entry and next block
710 Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
711 addPhiValues(Flow, Entry);
712 DT->changeImmediateDominator(Entry, Flow);
713
Christian Konig623977d2013-02-16 11:27:45 +0000714 PrevNode = Node;
715 while (!Order.empty() && !Visited.count(LoopEnd) &&
Tom Stellardf4e471a2013-02-08 22:24:38 +0000716 dominatesPredicates(Entry, Order.back())) {
Christian Konig623977d2013-02-16 11:27:45 +0000717 handleLoops(false, LoopEnd);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000718 }
719
Christian Konig623977d2013-02-16 11:27:45 +0000720 changeExit(PrevNode, Next, false);
721 setPrevNode(Next);
722 }
723}
724
725void AMDGPUStructurizeCFG::handleLoops(bool ExitUseAllowed,
726 BasicBlock *LoopEnd) {
727 RegionNode *Node = Order.back();
728 BasicBlock *LoopStart = Node->getEntry();
729
730 if (!Loops.count(LoopStart)) {
731 wireFlow(ExitUseAllowed, LoopEnd);
732 return;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000733 }
734
Christian Konig623977d2013-02-16 11:27:45 +0000735 if (!isPredictableTrue(Node))
736 LoopStart = needPrefix(true);
737
738 LoopEnd = Loops[Node->getEntry()];
739 wireFlow(false, LoopEnd);
740 while (!Visited.count(LoopEnd)) {
741 handleLoops(false, LoopEnd);
742 }
743
744 // Create an extra loop end node
745 LoopEnd = needPrefix(false);
746 BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
747 LoopConds.push_back(BranchInst::Create(Next, LoopStart,
748 BoolUndef, LoopEnd));
749 addPhiValues(LoopEnd, LoopStart);
750 setPrevNode(Next);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000751}
752
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000753/// After this function control flow looks like it should be, but
Tom Stellardf4e471a2013-02-08 22:24:38 +0000754/// branches and PHI nodes only have undefined conditions.
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000755void AMDGPUStructurizeCFG::createFlow() {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000756
757 BasicBlock *Exit = ParentRegion->getExit();
758 bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
759
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000760 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000761 AddedPhis.clear();
Tom Stellardf4e471a2013-02-08 22:24:38 +0000762 Conditions.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000763 LoopConds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000764
Christian Konig623977d2013-02-16 11:27:45 +0000765 PrevNode = 0;
766 Visited.clear();
767
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000768 while (!Order.empty()) {
Christian Konig623977d2013-02-16 11:27:45 +0000769 handleLoops(EntryDominatesExit, 0);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000770 }
771
Christian Konig623977d2013-02-16 11:27:45 +0000772 if (PrevNode)
773 changeExit(PrevNode, Exit, EntryDominatesExit);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000774 else
775 assert(EntryDominatesExit);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000776}
777
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000778/// Handle a rare case where the disintegrated nodes instructions
779/// no longer dominate all their uses. Not sure if this is really nessasary
780void AMDGPUStructurizeCFG::rebuildSSA() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000781 SSAUpdater Updater;
782 for (Region::block_iterator I = ParentRegion->block_begin(),
783 E = ParentRegion->block_end();
784 I != E; ++I) {
785
786 BasicBlock *BB = *I;
787 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
788 II != IE; ++II) {
789
790 bool Initialized = false;
791 for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {
792
793 Next = I->getNext();
794
795 Instruction *User = cast<Instruction>(I->getUser());
796 if (User->getParent() == BB) {
797 continue;
798
799 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
800 if (UserPN->getIncomingBlock(*I) == BB)
801 continue;
802 }
803
804 if (DT->dominates(II, User))
805 continue;
806
807 if (!Initialized) {
808 Value *Undef = UndefValue::get(II->getType());
809 Updater.Initialize(II->getType(), "");
810 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
811 Updater.AddAvailableValue(BB, II);
812 Initialized = true;
813 }
814 Updater.RewriteUseAfterInsertions(*I);
815 }
816 }
817 }
818}
819
820/// \brief Run the transformation for each region found
821bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000822 if (R->isTopLevelRegion())
823 return false;
824
825 Func = R->getEntry()->getParent();
826 ParentRegion = R;
827
828 DT = &getAnalysis<DominatorTree>();
829
830 orderNodes();
831 collectInfos();
832 createFlow();
Christian Konig623977d2013-02-16 11:27:45 +0000833 insertConditions(false);
834 insertConditions(true);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000835 setPhiValues();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000836 rebuildSSA();
837
Tom Stellard27f5d062013-02-08 22:24:37 +0000838 // Cleanup
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000839 Order.clear();
840 Visited.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000841 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000842 AddedPhis.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000843 Predicates.clear();
Tom Stellard27f5d062013-02-08 22:24:37 +0000844 Conditions.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000845 Loops.clear();
846 LoopPreds.clear();
847 LoopConds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000848
849 return true;
850}
851
852/// \brief Create the pass
853Pass *llvm::createAMDGPUStructurizeCFGPass() {
854 return new AMDGPUStructurizeCFG();
855}