blob: 26f842eb14fba45888871d7540380d49c0246ee6 [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"
Christian Konigef6b2482013-02-16 11:27:50 +000025#include "llvm/Support/PatternMatch.h"
Tom Stellard6b7d99d2012-12-19 22:10:31 +000026
27using namespace llvm;
Christian Konigef6b2482013-02-16 11:27:50 +000028using namespace llvm::PatternMatch;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000029
30namespace {
31
32// Definition of the complex types used in this pass.
33
34typedef std::pair<BasicBlock *, Value *> BBValuePair;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000035
36typedef SmallVector<RegionNode*, 8> RNVector;
37typedef SmallVector<BasicBlock*, 8> BBVector;
Tom Stellard27f5d062013-02-08 22:24:37 +000038typedef SmallVector<BranchInst*, 8> BranchVector;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000039typedef SmallVector<BBValuePair, 2> BBValueVector;
40
Tom Stellard27f5d062013-02-08 22:24:37 +000041typedef SmallPtrSet<BasicBlock *, 8> BBSet;
42
Tom Stellard6b7d99d2012-12-19 22:10:31 +000043typedef DenseMap<PHINode *, BBValueVector> PhiMap;
Christian Konigf0e469b2013-02-16 11:27:29 +000044typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000045typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
46typedef DenseMap<BasicBlock *, Value *> BBPredicates;
47typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
Christian Konig623977d2013-02-16 11:27:45 +000048typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
Tom Stellard13cf6cb2013-02-08 22:24:35 +000049typedef DenseMap<BasicBlock *, BBVector> BB2BBVecMap;
Tom Stellard6b7d99d2012-12-19 22:10:31 +000050
51// The name for newly created blocks.
52
53static const char *FlowBlockName = "Flow";
54
Christian Konigf0e469b2013-02-16 11:27:29 +000055/// @brief Find the nearest common dominator for multiple BasicBlocks
56///
57/// Helper class for AMDGPUStructurizeCFG
58/// TODO: Maybe move into common code
59class NearestCommonDominator {
60
61 DominatorTree *DT;
62
63 DTN2UnsignedMap IndexMap;
64
65 BasicBlock *Result;
66 unsigned ResultIndex;
67 bool ExplicitMentioned;
68
69public:
70 /// \brief Start a new query
71 NearestCommonDominator(DominatorTree *DomTree) {
72 DT = DomTree;
73 Result = 0;
74 }
75
76 /// \brief Add BB to the resulting dominator
77 void addBlock(BasicBlock *BB, bool Remember = true) {
78
79 DomTreeNode *Node = DT->getNode(BB);
80
81 if (Result == 0) {
82 unsigned Numbering = 0;
83 for (;Node;Node = Node->getIDom())
84 IndexMap[Node] = ++Numbering;
85 Result = BB;
86 ResultIndex = 1;
87 ExplicitMentioned = Remember;
88 return;
89 }
90
91 for (;Node;Node = Node->getIDom())
92 if (IndexMap.count(Node))
93 break;
94 else
95 IndexMap[Node] = 0;
96
97 assert(Node && "Dominator tree invalid!");
98
99 unsigned Numbering = IndexMap[Node];
100 if (Numbering > ResultIndex) {
101 Result = Node->getBlock();
102 ResultIndex = Numbering;
103 ExplicitMentioned = Remember && (Result == BB);
104 } else if (Numbering == ResultIndex) {
105 ExplicitMentioned |= Remember;
106 }
107 }
108
109 /// \brief Is "Result" one of the BBs added with "Remember" = True?
110 bool wasResultExplicitMentioned() {
111 return ExplicitMentioned;
112 }
113
114 /// \brief Get the query result
115 BasicBlock *getResult() {
116 return Result;
117 }
118};
119
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000120/// @brief Transforms the control flow graph on one single entry/exit region
121/// at a time.
122///
123/// After the transform all "If"/"Then"/"Else" style control flow looks like
124/// this:
125///
126/// \verbatim
127/// 1
128/// ||
129/// | |
130/// 2 |
131/// | /
132/// |/
133/// 3
134/// || Where:
135/// | | 1 = "If" block, calculates the condition
136/// 4 | 2 = "Then" subregion, runs if the condition is true
137/// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
138/// |/ 4 = "Else" optional subregion, runs if the condition is false
139/// 5 5 = "End" block, also rejoins the control flow
140/// \endverbatim
141///
142/// Control flow is expressed as a branch where the true exit goes into the
143/// "Then"/"Else" region, while the false exit skips the region
144/// The condition for the optional "Else" region is expressed as a PHI node.
145/// The incomming values of the PHI node are true for the "If" edge and false
146/// for the "Then" edge.
147///
148/// Additionally to that even complicated loops look like this:
149///
150/// \verbatim
151/// 1
152/// ||
153/// | |
154/// 2 ^ Where:
155/// | / 1 = "Entry" block
156/// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
157/// 3 3 = "Flow" block, with back edge to entry block
158/// |
159/// \endverbatim
160///
161/// The back edge of the "Flow" block is always on the false side of the branch
162/// while the true side continues the general flow. So the loop condition
163/// consist of a network of PHI nodes where the true incoming values expresses
164/// breaks and the false values expresses continue states.
165class AMDGPUStructurizeCFG : public RegionPass {
166
167 static char ID;
168
169 Type *Boolean;
170 ConstantInt *BoolTrue;
171 ConstantInt *BoolFalse;
172 UndefValue *BoolUndef;
173
174 Function *Func;
175 Region *ParentRegion;
176
177 DominatorTree *DT;
178
179 RNVector Order;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000180 BBSet Visited;
Christian Konig623977d2013-02-16 11:27:45 +0000181
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000182 BBPhiMap DeletedPhis;
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000183 BB2BBVecMap AddedPhis;
Christian Konig623977d2013-02-16 11:27:45 +0000184
185 PredMap Predicates;
Tom Stellard27f5d062013-02-08 22:24:37 +0000186 BranchVector Conditions;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000187
Christian Konig623977d2013-02-16 11:27:45 +0000188 BB2BBMap Loops;
189 PredMap LoopPreds;
190 BranchVector LoopConds;
191
192 RegionNode *PrevNode;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000193
194 void orderNodes();
195
Christian Konig623977d2013-02-16 11:27:45 +0000196 void analyzeLoops(RegionNode *N);
197
Christian Konigef6b2482013-02-16 11:27:50 +0000198 Value *invert(Value *Condition);
199
Tom Stellard27f5d062013-02-08 22:24:37 +0000200 Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000201
Christian Konig623977d2013-02-16 11:27:45 +0000202 void gatherPredicates(RegionNode *N);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000203
204 void collectInfos();
205
Christian Konig623977d2013-02-16 11:27:45 +0000206 void insertConditions(bool Loops);
Tom Stellard27f5d062013-02-08 22:24:37 +0000207
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000208 void delPhiValues(BasicBlock *From, BasicBlock *To);
209
210 void addPhiValues(BasicBlock *From, BasicBlock *To);
211
212 void setPhiValues();
213
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000214 void killTerminator(BasicBlock *BB);
215
Tom Stellardf4e471a2013-02-08 22:24:38 +0000216 void changeExit(RegionNode *Node, BasicBlock *NewExit,
217 bool IncludeDominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000218
Tom Stellardf4e471a2013-02-08 22:24:38 +0000219 BasicBlock *getNextFlow(BasicBlock *Dominator);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000220
Christian Konig623977d2013-02-16 11:27:45 +0000221 BasicBlock *needPrefix(bool NeedEmpty);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000222
Tom Stellardf4e471a2013-02-08 22:24:38 +0000223 BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
224
Christian Konig623977d2013-02-16 11:27:45 +0000225 void setPrevNode(BasicBlock *BB);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000226
227 bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
228
Christian Konig623977d2013-02-16 11:27:45 +0000229 bool isPredictableTrue(RegionNode *Node);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000230
Christian Konig623977d2013-02-16 11:27:45 +0000231 void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
232
233 void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000234
235 void createFlow();
236
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000237 void rebuildSSA();
238
239public:
240 AMDGPUStructurizeCFG():
241 RegionPass(ID) {
242
243 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
244 }
245
246 virtual bool doInitialization(Region *R, RGPassManager &RGM);
247
248 virtual bool runOnRegion(Region *R, RGPassManager &RGM);
249
250 virtual const char *getPassName() const {
251 return "AMDGPU simplify control flow";
252 }
253
254 void getAnalysisUsage(AnalysisUsage &AU) const {
255
256 AU.addRequired<DominatorTree>();
257 AU.addPreserved<DominatorTree>();
258 RegionPass::getAnalysisUsage(AU);
259 }
260
261};
262
263} // end anonymous namespace
264
265char AMDGPUStructurizeCFG::ID = 0;
266
267/// \brief Initialize the types and constants used in the pass
268bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000269 LLVMContext &Context = R->getEntry()->getContext();
270
271 Boolean = Type::getInt1Ty(Context);
272 BoolTrue = ConstantInt::getTrue(Context);
273 BoolFalse = ConstantInt::getFalse(Context);
274 BoolUndef = UndefValue::get(Boolean);
275
276 return false;
277}
278
279/// \brief Build up the general order of nodes
280void AMDGPUStructurizeCFG::orderNodes() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000281 scc_iterator<Region *> I = scc_begin(ParentRegion),
282 E = scc_end(ParentRegion);
283 for (Order.clear(); I != E; ++I) {
284 std::vector<RegionNode *> &Nodes = *I;
285 Order.append(Nodes.begin(), Nodes.end());
286 }
287}
288
Christian Konig623977d2013-02-16 11:27:45 +0000289/// \brief Determine the end of the loops
290void AMDGPUStructurizeCFG::analyzeLoops(RegionNode *N) {
291
292 if (N->isSubRegion()) {
293 // Test for exit as back edge
294 BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
295 if (Visited.count(Exit))
296 Loops[Exit] = N->getEntry();
297
298 } else {
299 // Test for sucessors as back edge
300 BasicBlock *BB = N->getNodeAs<BasicBlock>();
301 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
302
303 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
304 BasicBlock *Succ = Term->getSuccessor(i);
305
306 if (Visited.count(Succ))
307 Loops[Succ] = BB;
308 }
309 }
310}
311
Christian Konigef6b2482013-02-16 11:27:50 +0000312/// \brief Invert the given condition
313Value *AMDGPUStructurizeCFG::invert(Value *Condition) {
314
315 // First: Check if it's a constant
316 if (Condition == BoolTrue)
317 return BoolFalse;
318
319 if (Condition == BoolFalse)
320 return BoolTrue;
321
322 if (Condition == BoolUndef)
323 return BoolUndef;
324
325 // Second: If the condition is already inverted, return the original value
326 if (match(Condition, m_Not(m_Value(Condition))))
327 return Condition;
328
329 // Third: Check all the users for an invert
330 BasicBlock *Parent = cast<Instruction>(Condition)->getParent();
331 for (Value::use_iterator I = Condition->use_begin(),
332 E = Condition->use_end(); I != E; ++I) {
333
334 Instruction *User = dyn_cast<Instruction>(*I);
335 if (!User || User->getParent() != Parent)
336 continue;
337
338 if (match(*I, m_Not(m_Specific(Condition))))
339 return *I;
340 }
341
342 // Last option: Create a new instruction
343 return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
344}
345
Tom Stellard27f5d062013-02-08 22:24:37 +0000346/// \brief Build the condition for one edge
347Value *AMDGPUStructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
348 bool Invert) {
349 Value *Cond = Invert ? BoolFalse : BoolTrue;
350 if (Term->isConditional()) {
351 Cond = Term->getCondition();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000352
Tom Stellard27f5d062013-02-08 22:24:37 +0000353 if (Idx != Invert)
Christian Konigef6b2482013-02-16 11:27:50 +0000354 Cond = invert(Cond);
Tom Stellard27f5d062013-02-08 22:24:37 +0000355 }
356 return Cond;
357}
358
Tom Stellard27f5d062013-02-08 22:24:37 +0000359/// \brief Analyze the predecessors of each block and build up predicates
Christian Konig623977d2013-02-16 11:27:45 +0000360void AMDGPUStructurizeCFG::gatherPredicates(RegionNode *N) {
361
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000362 RegionInfo *RI = ParentRegion->getRegionInfo();
Tom Stellard27f5d062013-02-08 22:24:37 +0000363 BasicBlock *BB = N->getEntry();
364 BBPredicates &Pred = Predicates[BB];
Christian Konig623977d2013-02-16 11:27:45 +0000365 BBPredicates &LPred = LoopPreds[BB];
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000366
Tom Stellard27f5d062013-02-08 22:24:37 +0000367 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
368 PI != PE; ++PI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000369
Christian Konig623977d2013-02-16 11:27:45 +0000370 // Ignore it if it's a branch from outside into our region entry
371 if (!ParentRegion->contains(*PI))
Tom Stellard27f5d062013-02-08 22:24:37 +0000372 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000373
Tom Stellard27f5d062013-02-08 22:24:37 +0000374 Region *R = RI->getRegionFor(*PI);
375 if (R == ParentRegion) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000376
Tom Stellard27f5d062013-02-08 22:24:37 +0000377 // It's a top level block in our region
378 BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
379 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
380 BasicBlock *Succ = Term->getSuccessor(i);
381 if (Succ != BB)
382 continue;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000383
Tom Stellard27f5d062013-02-08 22:24:37 +0000384 if (Visited.count(*PI)) {
385 // Normal forward edge
386 if (Term->isConditional()) {
387 // Try to treat it like an ELSE block
388 BasicBlock *Other = Term->getSuccessor(!i);
Christian Konig623977d2013-02-16 11:27:45 +0000389 if (Visited.count(Other) && !Loops.count(Other) &&
Tom Stellard27f5d062013-02-08 22:24:37 +0000390 !Pred.count(Other) && !Pred.count(*PI)) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000391
Tom Stellard27f5d062013-02-08 22:24:37 +0000392 Pred[Other] = BoolFalse;
393 Pred[*PI] = BoolTrue;
394 continue;
395 }
396 }
Christian Konig623977d2013-02-16 11:27:45 +0000397 Pred[*PI] = buildCondition(Term, i, false);
398
Tom Stellard27f5d062013-02-08 22:24:37 +0000399 } else {
400 // Back edge
Christian Konig623977d2013-02-16 11:27:45 +0000401 LPred[*PI] = buildCondition(Term, i, true);
Tom Stellard27f5d062013-02-08 22:24:37 +0000402 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000403 }
404
405 } else {
406
407 // It's an exit from a sub region
408 while(R->getParent() != ParentRegion)
409 R = R->getParent();
410
411 // Edge from inside a subregion to its entry, ignore it
412 if (R == N)
413 continue;
414
415 BasicBlock *Entry = R->getEntry();
Christian Konig623977d2013-02-16 11:27:45 +0000416 if (Visited.count(Entry))
417 Pred[Entry] = BoolTrue;
418 else
419 LPred[Entry] = BoolFalse;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000420 }
421 }
422}
423
424/// \brief Collect various loop and predicate infos
425void AMDGPUStructurizeCFG::collectInfos() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000426
427 // Reset predicate
428 Predicates.clear();
429
430 // and loop infos
Christian Konig623977d2013-02-16 11:27:45 +0000431 Loops.clear();
432 LoopPreds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000433
Tom Stellard27f5d062013-02-08 22:24:37 +0000434 // Reset the visited nodes
435 Visited.clear();
436
437 for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
438 OI != OE; ++OI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000439
440 // Analyze all the conditions leading to a node
Christian Konig623977d2013-02-16 11:27:45 +0000441 gatherPredicates(*OI);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000442
Tom Stellard27f5d062013-02-08 22:24:37 +0000443 // Remember that we've seen this node
Tom Stellardf4e471a2013-02-08 22:24:38 +0000444 Visited.insert((*OI)->getEntry());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000445
Christian Konig623977d2013-02-16 11:27:45 +0000446 // Find the last back edges
447 analyzeLoops(*OI);
Tom Stellard27f5d062013-02-08 22:24:37 +0000448 }
Tom Stellard27f5d062013-02-08 22:24:37 +0000449}
450
451/// \brief Insert the missing branch conditions
Christian Konig623977d2013-02-16 11:27:45 +0000452void AMDGPUStructurizeCFG::insertConditions(bool Loops) {
453 BranchVector &Conds = Loops ? LoopConds : Conditions;
454 Value *Default = Loops ? BoolTrue : BoolFalse;
Tom Stellard27f5d062013-02-08 22:24:37 +0000455 SSAUpdater PhiInserter;
456
Christian Konig623977d2013-02-16 11:27:45 +0000457 for (BranchVector::iterator I = Conds.begin(),
458 E = Conds.end(); I != E; ++I) {
Tom Stellard27f5d062013-02-08 22:24:37 +0000459
460 BranchInst *Term = *I;
Tom Stellard27f5d062013-02-08 22:24:37 +0000461 assert(Term->isConditional());
462
Christian Konig623977d2013-02-16 11:27:45 +0000463 BasicBlock *Parent = Term->getParent();
464 BasicBlock *SuccTrue = Term->getSuccessor(0);
465 BasicBlock *SuccFalse = Term->getSuccessor(1);
Tom Stellard27f5d062013-02-08 22:24:37 +0000466
Christian Konig25bd8842013-02-16 11:27:40 +0000467 PhiInserter.Initialize(Boolean, "");
468 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
Christian Konig623977d2013-02-16 11:27:45 +0000469 PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
Christian Konig25bd8842013-02-16 11:27:40 +0000470
Christian Konig623977d2013-02-16 11:27:45 +0000471 BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
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
Christian Konig623977d2013-02-16 11:27:45 +0000651BasicBlock *AMDGPUStructurizeCFG::needPrefix(bool NeedEmpty) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000652
Christian Konig623977d2013-02-16 11:27:45 +0000653 BasicBlock *Entry = PrevNode->getEntry();
Tom Stellardf4e471a2013-02-08 22:24:38 +0000654
Christian Konig623977d2013-02-16 11:27:45 +0000655 if (!PrevNode->isSubRegion()) {
656 killTerminator(Entry);
657 if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
658 return Entry;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000659
Christian Konig623977d2013-02-16 11:27:45 +0000660 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000661
Christian Konig623977d2013-02-16 11:27:45 +0000662 // create a new flow node
663 BasicBlock *Flow = getNextFlow(Entry);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000664
Christian Konig623977d2013-02-16 11:27:45 +0000665 // and wire it up
666 changeExit(PrevNode, Flow, true);
667 PrevNode = ParentRegion->getBBNode(Flow);
668 return Flow;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000669}
670
671/// \brief Returns the region exit if possible, otherwise just a new flow node
672BasicBlock *AMDGPUStructurizeCFG::needPostfix(BasicBlock *Flow,
673 bool ExitUseAllowed) {
674
675 if (Order.empty() && ExitUseAllowed) {
676 BasicBlock *Exit = ParentRegion->getExit();
677 DT->changeImmediateDominator(Exit, Flow);
678 addPhiValues(Flow, Exit);
679 return Exit;
680 }
681 return getNextFlow(Flow);
682}
683
Christian Konig623977d2013-02-16 11:27:45 +0000684/// \brief Set the previous node
685void AMDGPUStructurizeCFG::setPrevNode(BasicBlock *BB) {
686 PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) : 0;
Tom Stellardf4e471a2013-02-08 22:24:38 +0000687}
688
689/// \brief Does BB dominate all the predicates of Node ?
690bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
691 BBPredicates &Preds = Predicates[Node->getEntry()];
692 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
693 PI != PE; ++PI) {
694
695 if (!DT->dominates(BB, PI->first))
696 return false;
697 }
698 return true;
699}
700
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000701/// \brief Can we predict that this node will always be called?
Christian Konig623977d2013-02-16 11:27:45 +0000702bool AMDGPUStructurizeCFG::isPredictableTrue(RegionNode *Node) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000703
Christian Konig623977d2013-02-16 11:27:45 +0000704 BBPredicates &Preds = Predicates[Node->getEntry()];
705 bool Dominated = false;
706
707 // Regionentry is always true
708 if (PrevNode == 0)
709 return true;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000710
711 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
712 I != E; ++I) {
713
714 if (I->second != BoolTrue)
715 return false;
716
Christian Konig623977d2013-02-16 11:27:45 +0000717 if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000718 Dominated = true;
719 }
Tom Stellardf4e471a2013-02-08 22:24:38 +0000720
721 // TODO: The dominator check is too strict
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000722 return Dominated;
723}
724
Tom Stellardf4e471a2013-02-08 22:24:38 +0000725/// Take one node from the order vector and wire it up
Christian Konig623977d2013-02-16 11:27:45 +0000726void AMDGPUStructurizeCFG::wireFlow(bool ExitUseAllowed,
727 BasicBlock *LoopEnd) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000728
Tom Stellardf4e471a2013-02-08 22:24:38 +0000729 RegionNode *Node = Order.pop_back_val();
Christian Konig623977d2013-02-16 11:27:45 +0000730 Visited.insert(Node->getEntry());
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000731
Christian Konig623977d2013-02-16 11:27:45 +0000732 if (isPredictableTrue(Node)) {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000733 // Just a linear flow
Christian Konig623977d2013-02-16 11:27:45 +0000734 if (PrevNode) {
735 changeExit(PrevNode, Node->getEntry(), true);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000736 }
Christian Konig623977d2013-02-16 11:27:45 +0000737 PrevNode = Node;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000738
739 } else {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000740 // Insert extra prefix node (or reuse last one)
Christian Konig623977d2013-02-16 11:27:45 +0000741 BasicBlock *Flow = needPrefix(false);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000742
Tom Stellardf4e471a2013-02-08 22:24:38 +0000743 // Insert extra postfix node (or use exit instead)
744 BasicBlock *Entry = Node->getEntry();
Christian Konig623977d2013-02-16 11:27:45 +0000745 BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000746
747 // let it point to entry and next block
748 Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
749 addPhiValues(Flow, Entry);
750 DT->changeImmediateDominator(Entry, Flow);
751
Christian Konig623977d2013-02-16 11:27:45 +0000752 PrevNode = Node;
753 while (!Order.empty() && !Visited.count(LoopEnd) &&
Tom Stellardf4e471a2013-02-08 22:24:38 +0000754 dominatesPredicates(Entry, Order.back())) {
Christian Konig623977d2013-02-16 11:27:45 +0000755 handleLoops(false, LoopEnd);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000756 }
757
Christian Konig623977d2013-02-16 11:27:45 +0000758 changeExit(PrevNode, Next, false);
759 setPrevNode(Next);
760 }
761}
762
763void AMDGPUStructurizeCFG::handleLoops(bool ExitUseAllowed,
764 BasicBlock *LoopEnd) {
765 RegionNode *Node = Order.back();
766 BasicBlock *LoopStart = Node->getEntry();
767
768 if (!Loops.count(LoopStart)) {
769 wireFlow(ExitUseAllowed, LoopEnd);
770 return;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000771 }
772
Christian Konig623977d2013-02-16 11:27:45 +0000773 if (!isPredictableTrue(Node))
774 LoopStart = needPrefix(true);
775
776 LoopEnd = Loops[Node->getEntry()];
777 wireFlow(false, LoopEnd);
778 while (!Visited.count(LoopEnd)) {
779 handleLoops(false, LoopEnd);
780 }
781
782 // Create an extra loop end node
783 LoopEnd = needPrefix(false);
784 BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
785 LoopConds.push_back(BranchInst::Create(Next, LoopStart,
786 BoolUndef, LoopEnd));
787 addPhiValues(LoopEnd, LoopStart);
788 setPrevNode(Next);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000789}
790
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000791/// After this function control flow looks like it should be, but
Tom Stellardf4e471a2013-02-08 22:24:38 +0000792/// branches and PHI nodes only have undefined conditions.
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000793void AMDGPUStructurizeCFG::createFlow() {
Tom Stellardf4e471a2013-02-08 22:24:38 +0000794
795 BasicBlock *Exit = ParentRegion->getExit();
796 bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
797
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000798 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000799 AddedPhis.clear();
Tom Stellardf4e471a2013-02-08 22:24:38 +0000800 Conditions.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000801 LoopConds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000802
Christian Konig623977d2013-02-16 11:27:45 +0000803 PrevNode = 0;
804 Visited.clear();
805
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000806 while (!Order.empty()) {
Christian Konig623977d2013-02-16 11:27:45 +0000807 handleLoops(EntryDominatesExit, 0);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000808 }
809
Christian Konig623977d2013-02-16 11:27:45 +0000810 if (PrevNode)
811 changeExit(PrevNode, Exit, EntryDominatesExit);
Tom Stellardf4e471a2013-02-08 22:24:38 +0000812 else
813 assert(EntryDominatesExit);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000814}
815
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000816/// Handle a rare case where the disintegrated nodes instructions
817/// no longer dominate all their uses. Not sure if this is really nessasary
818void AMDGPUStructurizeCFG::rebuildSSA() {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000819 SSAUpdater Updater;
820 for (Region::block_iterator I = ParentRegion->block_begin(),
821 E = ParentRegion->block_end();
822 I != E; ++I) {
823
824 BasicBlock *BB = *I;
825 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
826 II != IE; ++II) {
827
828 bool Initialized = false;
829 for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {
830
831 Next = I->getNext();
832
833 Instruction *User = cast<Instruction>(I->getUser());
834 if (User->getParent() == BB) {
835 continue;
836
837 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
838 if (UserPN->getIncomingBlock(*I) == BB)
839 continue;
840 }
841
842 if (DT->dominates(II, User))
843 continue;
844
845 if (!Initialized) {
846 Value *Undef = UndefValue::get(II->getType());
847 Updater.Initialize(II->getType(), "");
848 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
849 Updater.AddAvailableValue(BB, II);
850 Initialized = true;
851 }
852 Updater.RewriteUseAfterInsertions(*I);
853 }
854 }
855 }
856}
857
858/// \brief Run the transformation for each region found
859bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000860 if (R->isTopLevelRegion())
861 return false;
862
863 Func = R->getEntry()->getParent();
864 ParentRegion = R;
865
866 DT = &getAnalysis<DominatorTree>();
867
868 orderNodes();
869 collectInfos();
870 createFlow();
Christian Konig623977d2013-02-16 11:27:45 +0000871 insertConditions(false);
872 insertConditions(true);
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000873 setPhiValues();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000874 rebuildSSA();
875
Tom Stellard27f5d062013-02-08 22:24:37 +0000876 // Cleanup
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000877 Order.clear();
878 Visited.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000879 DeletedPhis.clear();
Tom Stellard13cf6cb2013-02-08 22:24:35 +0000880 AddedPhis.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000881 Predicates.clear();
Tom Stellard27f5d062013-02-08 22:24:37 +0000882 Conditions.clear();
Christian Konig623977d2013-02-16 11:27:45 +0000883 Loops.clear();
884 LoopPreds.clear();
885 LoopConds.clear();
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000886
887 return true;
888}
889
890/// \brief Create the pass
891Pass *llvm::createAMDGPUStructurizeCFGPass() {
892 return new AMDGPUStructurizeCFG();
893}