blob: ae182bd947d0c61529686e886f75f2d822062c53 [file] [log] [blame]
Tom Stellardf8794352012-12-19 22:10:31 +00001//===-- SIAnnotateControlFlow.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/// Annotates the control flow with hardware specific intrinsics.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
Tom Stellardf8794352012-12-19 22:10:31 +000016#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellardbc4497b2016-02-12 23:45:29 +000017#include "llvm/Analysis/DivergenceAnalysis.h"
Tom Stellard0f29de72015-02-05 15:32:15 +000018#include "llvm/Analysis/LoopInfo.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000019#include "llvm/IR/Constants.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000020#include "llvm/IR/Dominators.h"
Tom Stellardb0804ec2013-06-07 20:28:43 +000021#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000023#include "llvm/Pass.h"
24#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Tom Stellardf8794352012-12-19 22:10:31 +000025#include "llvm/Transforms/Utils/SSAUpdater.h"
26
27using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "si-annotate-control-flow"
30
Tom Stellardf8794352012-12-19 22:10:31 +000031namespace {
32
33// Complex types used in this pass
34typedef std::pair<BasicBlock *, Value *> StackEntry;
35typedef SmallVector<StackEntry, 16> StackVector;
36
37// Intrinsic names the control flow is annotated with
Matt Arsenault7898b902016-01-22 18:42:55 +000038static const char *const IfIntrinsic = "llvm.amdgcn.if";
39static const char *const ElseIntrinsic = "llvm.amdgcn.else";
Matt Arsenault48d70cb2016-07-09 17:18:39 +000040static const char *const BreakIntrinsic = "llvm.amdgcn.break";
Matt Arsenault7898b902016-01-22 18:42:55 +000041static const char *const IfBreakIntrinsic = "llvm.amdgcn.if.break";
42static const char *const ElseBreakIntrinsic = "llvm.amdgcn.else.break";
43static const char *const LoopIntrinsic = "llvm.amdgcn.loop";
44static const char *const EndCfIntrinsic = "llvm.amdgcn.end.cf";
Tom Stellardf8794352012-12-19 22:10:31 +000045
46class SIAnnotateControlFlow : public FunctionPass {
Tom Stellardbc4497b2016-02-12 23:45:29 +000047 DivergenceAnalysis *DA;
Tom Stellardf8794352012-12-19 22:10:31 +000048
Tom Stellardf8794352012-12-19 22:10:31 +000049 Type *Boolean;
50 Type *Void;
51 Type *Int64;
52 Type *ReturnStruct;
53
54 ConstantInt *BoolTrue;
55 ConstantInt *BoolFalse;
56 UndefValue *BoolUndef;
57 Constant *Int64Zero;
58
59 Constant *If;
60 Constant *Else;
61 Constant *Break;
62 Constant *IfBreak;
63 Constant *ElseBreak;
64 Constant *Loop;
65 Constant *EndCf;
66
67 DominatorTree *DT;
68 StackVector Stack;
Tom Stellardf8794352012-12-19 22:10:31 +000069
Tom Stellard0f29de72015-02-05 15:32:15 +000070 LoopInfo *LI;
71
Nicolai Haehnle05b127d2016-04-14 17:42:35 +000072 bool isUniform(BranchInst *T);
73
Tom Stellardf8794352012-12-19 22:10:31 +000074 bool isTopOfStack(BasicBlock *BB);
75
76 Value *popSaved();
77
78 void push(BasicBlock *BB, Value *Saved);
79
80 bool isElse(PHINode *Phi);
81
82 void eraseIfUnused(PHINode *Phi);
83
84 void openIf(BranchInst *Term);
85
86 void insertElse(BranchInst *Term);
87
Changpeng Fange07f1aa2016-02-12 17:11:04 +000088 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
89 llvm::Loop *L, BranchInst *Term);
Tom Stellardf8794352012-12-19 22:10:31 +000090
91 void handleLoop(BranchInst *Term);
92
93 void closeControlFlow(BasicBlock *BB);
94
95public:
Tom Stellard77a17772016-01-20 15:48:27 +000096 static char ID;
97
Tom Stellardf8794352012-12-19 22:10:31 +000098 SIAnnotateControlFlow():
99 FunctionPass(ID) { }
100
Craig Topper5656db42014-04-29 07:57:24 +0000101 bool doInitialization(Module &M) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000102
Craig Topper5656db42014-04-29 07:57:24 +0000103 bool runOnFunction(Function &F) override;
Tom Stellardf8794352012-12-19 22:10:31 +0000104
Mehdi Amini117296c2016-10-01 02:56:57 +0000105 StringRef getPassName() const override { return "SI annotate control flow"; }
Tom Stellardf8794352012-12-19 22:10:31 +0000106
Craig Topper5656db42014-04-29 07:57:24 +0000107 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0f29de72015-02-05 15:32:15 +0000108 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000109 AU.addRequired<DominatorTreeWrapperPass>();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000110 AU.addRequired<DivergenceAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000111 AU.addPreserved<DominatorTreeWrapperPass>();
Tom Stellardf8794352012-12-19 22:10:31 +0000112 FunctionPass::getAnalysisUsage(AU);
113 }
114
115};
116
117} // end anonymous namespace
118
Tom Stellard77a17772016-01-20 15:48:27 +0000119INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
120 "Annotate SI Control Flow", false, false)
Matt Arsenault31a58c62017-03-02 23:50:51 +0000121INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Tom Stellardbc4497b2016-02-12 23:45:29 +0000122INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
Tom Stellard77a17772016-01-20 15:48:27 +0000123INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
124 "Annotate SI Control Flow", false, false)
125
Tom Stellardf8794352012-12-19 22:10:31 +0000126char SIAnnotateControlFlow::ID = 0;
127
128/// \brief Initialize all the types and constants used in the pass
129bool SIAnnotateControlFlow::doInitialization(Module &M) {
Tom Stellardf8794352012-12-19 22:10:31 +0000130 LLVMContext &Context = M.getContext();
131
132 Void = Type::getVoidTy(Context);
133 Boolean = Type::getInt1Ty(Context);
134 Int64 = Type::getInt64Ty(Context);
Craig Topper062a2ba2014-04-25 05:30:21 +0000135 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000136
137 BoolTrue = ConstantInt::getTrue(Context);
138 BoolFalse = ConstantInt::getFalse(Context);
139 BoolUndef = UndefValue::get(Boolean);
140 Int64Zero = ConstantInt::get(Int64, 0);
141
142 If = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000143 IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000144
145 Else = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000146 ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000147
Matt Arsenault48d70cb2016-07-09 17:18:39 +0000148 Break = M.getOrInsertFunction(
149 BreakIntrinsic, Int64, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000150 cast<Function>(Break)->setDoesNotAccessMemory();
Matt Arsenault48d70cb2016-07-09 17:18:39 +0000151
Tom Stellardf8794352012-12-19 22:10:31 +0000152 IfBreak = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000154 cast<Function>(IfBreak)->setDoesNotAccessMemory();;
Tom Stellardf8794352012-12-19 22:10:31 +0000155
156 ElseBreak = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000157 ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr);
Matt Arsenault6408c912016-09-16 22:11:18 +0000158 cast<Function>(ElseBreak)->setDoesNotAccessMemory();
Tom Stellardf8794352012-12-19 22:10:31 +0000159
160 Loop = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000161 LoopIntrinsic, Boolean, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000162
163 EndCf = M.getOrInsertFunction(
Craig Topper062a2ba2014-04-25 05:30:21 +0000164 EndCfIntrinsic, Void, Int64, (Type *)nullptr);
Tom Stellardf8794352012-12-19 22:10:31 +0000165
166 return false;
167}
168
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000169/// \brief Is the branch condition uniform or did the StructurizeCFG pass
170/// consider it as such?
171bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
172 return DA->isUniform(T->getCondition()) ||
173 T->getMetadata("structurizecfg.uniform") != nullptr;
174}
175
Tom Stellardf8794352012-12-19 22:10:31 +0000176/// \brief Is BB the last block saved on the stack ?
177bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
Michel Danzerae0a4032013-02-14 08:00:33 +0000178 return !Stack.empty() && Stack.back().first == BB;
Tom Stellardf8794352012-12-19 22:10:31 +0000179}
180
181/// \brief Pop the last saved value from the control flow stack
182Value *SIAnnotateControlFlow::popSaved() {
183 return Stack.pop_back_val().second;
184}
185
186/// \brief Push a BB and saved value to the control flow stack
187void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
188 Stack.push_back(std::make_pair(BB, Saved));
189}
190
191/// \brief Can the condition represented by this PHI node treated like
192/// an "Else" block?
193bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
Tom Stellardf8794352012-12-19 22:10:31 +0000194 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
195 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
196 if (Phi->getIncomingBlock(i) == IDom) {
197
198 if (Phi->getIncomingValue(i) != BoolTrue)
199 return false;
200
201 } else {
202 if (Phi->getIncomingValue(i) != BoolFalse)
203 return false;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000204
Tom Stellardf8794352012-12-19 22:10:31 +0000205 }
206 }
207 return true;
208}
209
210// \brief Erase "Phi" if it is not used any more
211void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
212 if (!Phi->hasNUsesOrMore(1))
213 Phi->eraseFromParent();
214}
215
216/// \brief Open a new "If" block
217void SIAnnotateControlFlow::openIf(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000218 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000219 return;
220 }
Tom Stellardf8794352012-12-19 22:10:31 +0000221 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
222 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
223 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
224}
225
226/// \brief Close the last "If" block and open a new "Else" block
227void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000228 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000229 return;
230 }
Tom Stellardf8794352012-12-19 22:10:31 +0000231 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
232 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
233 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
234}
235
236/// \brief Recursively handle the condition leading to a loop
Tom Stellardd4a19502015-04-14 14:36:45 +0000237Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken,
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000238 llvm::Loop *L, BranchInst *Term) {
Tom Stellard0b7feb12015-05-01 03:44:08 +0000239
240 // Only search through PHI nodes which are inside the loop. If we try this
241 // with PHI nodes that are outside of the loop, we end up inserting new PHI
242 // nodes outside of the loop which depend on values defined inside the loop.
243 // This will break the module with
244 // 'Instruction does not dominate all users!' errors.
245 PHINode *Phi = nullptr;
246 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) {
247
Tom Stellardde16a2e2014-06-20 17:06:02 +0000248 BasicBlock *Parent = Phi->getParent();
249 PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front());
250 Value *Ret = NewPhi;
Tom Stellardf8794352012-12-19 22:10:31 +0000251
Alp Tokerf907b892013-12-05 05:44:44 +0000252 // Handle all non-constant incoming values first
Tom Stellardf8794352012-12-19 22:10:31 +0000253 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
254 Value *Incoming = Phi->getIncomingValue(i);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000255 BasicBlock *From = Phi->getIncomingBlock(i);
256 if (isa<ConstantInt>(Incoming)) {
257 NewPhi->addIncoming(Broken, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000258 continue;
Tom Stellardde16a2e2014-06-20 17:06:02 +0000259 }
Tom Stellardf8794352012-12-19 22:10:31 +0000260
261 Phi->setIncomingValue(i, BoolFalse);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000262 Value *PhiArg = handleLoopCondition(Incoming, Broken, L, Term);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000263 NewPhi->addIncoming(PhiArg, From);
Tom Stellardf8794352012-12-19 22:10:31 +0000264 }
265
Tom Stellardf8794352012-12-19 22:10:31 +0000266 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
267
268 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
269
270 Value *Incoming = Phi->getIncomingValue(i);
271 if (Incoming != BoolTrue)
272 continue;
273
274 BasicBlock *From = Phi->getIncomingBlock(i);
275 if (From == IDom) {
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000276 // We're in the following situation:
277 // IDom/From
278 // | \
279 // | If-block
280 // | /
281 // Parent
282 // where we want to break out of the loop if the If-block is not taken.
283 // Due to the depth-first traversal, there should be an end.cf
284 // intrinsic in Parent, and we insert an else.break before it.
285 //
286 // Note that the end.cf need not be the first non-phi instruction
287 // of parent, particularly when we're dealing with a multi-level
288 // break, but it should occur within a group of intrinsic calls
289 // at the beginning of the block.
Tom Stellardf8794352012-12-19 22:10:31 +0000290 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt());
Nicolai Haehnle279970c2016-04-12 16:10:38 +0000291 while (OldEnd && OldEnd->getCalledFunction() != EndCf)
292 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode());
Tom Stellardf8794352012-12-19 22:10:31 +0000293 if (OldEnd && OldEnd->getCalledFunction() == EndCf) {
Tom Stellardde16a2e2014-06-20 17:06:02 +0000294 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi };
295 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd);
Tom Stellardf8794352012-12-19 22:10:31 +0000296 continue;
297 }
298 }
Tom Stellardf8794352012-12-19 22:10:31 +0000299 TerminatorInst *Insert = From->getTerminator();
Tom Stellardde16a2e2014-06-20 17:06:02 +0000300 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
301 NewPhi->setIncomingValue(i, PhiArg);
Tom Stellardf8794352012-12-19 22:10:31 +0000302 }
303 eraseIfUnused(Phi);
Tom Stellardde16a2e2014-06-20 17:06:02 +0000304 return Ret;
Tom Stellardf8794352012-12-19 22:10:31 +0000305
306 } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
307 BasicBlock *Parent = Inst->getParent();
Tom Stellardd4a19502015-04-14 14:36:45 +0000308 Instruction *Insert;
309 if (L->contains(Inst)) {
310 Insert = Parent->getTerminator();
311 } else {
312 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
313 }
Tom Stellardde16a2e2014-06-20 17:06:02 +0000314 Value *Args[] = { Cond, Broken };
315 return CallInst::Create(IfBreak, Args, "", Insert);
Tom Stellardf8794352012-12-19 22:10:31 +0000316
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000317 // Insert IfBreak before TERM for constant COND.
318 } else if (isa<ConstantInt>(Cond)) {
319 Value *Args[] = { Cond, Broken };
320 return CallInst::Create(IfBreak, Args, "", Term);
321
Tom Stellardf8794352012-12-19 22:10:31 +0000322 } else {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000323 llvm_unreachable("Unhandled loop condition!");
Tom Stellardf8794352012-12-19 22:10:31 +0000324 }
Matt Arsenault37fefd62016-06-10 02:18:02 +0000325 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000326}
327
328/// \brief Handle a back edge (loop)
329void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000330 if (isUniform(Term)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000331 return;
332 }
333
Tom Stellardd4a19502015-04-14 14:36:45 +0000334 BasicBlock *BB = Term->getParent();
335 llvm::Loop *L = LI->getLoopFor(BB);
Changpeng Fang26fb9d22016-07-28 23:01:45 +0000336 if (!L)
337 return;
Tom Stellardf8794352012-12-19 22:10:31 +0000338 BasicBlock *Target = Term->getSuccessor(1);
339 PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front());
340
Tom Stellardf8794352012-12-19 22:10:31 +0000341 Value *Cond = Term->getCondition();
342 Term->setCondition(BoolTrue);
Changpeng Fange07f1aa2016-02-12 17:11:04 +0000343 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
Tom Stellardf8794352012-12-19 22:10:31 +0000344
Tom Stellardf8794352012-12-19 22:10:31 +0000345 for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target);
346 PI != PE; ++PI) {
347
348 Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI);
349 }
350
351 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
352 push(Term->getSuccessor(0), Arg);
Tom Stellard0f29de72015-02-05 15:32:15 +0000353}/// \brief Close the last opened control flow
Tom Stellardf8794352012-12-19 22:10:31 +0000354void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
Tom Stellard0f29de72015-02-05 15:32:15 +0000355 llvm::Loop *L = LI->getLoopFor(BB);
356
Nicolai Haehnle19f0f512016-04-14 17:42:18 +0000357 assert(Stack.back().first == BB);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000358
Tom Stellard0f29de72015-02-05 15:32:15 +0000359 if (L && L->getHeader() == BB) {
360 // We can't insert an EndCF call into a loop header, because it will
361 // get executed on every iteration of the loop, when it should be
362 // executed only once before the loop.
363 SmallVector <BasicBlock*, 8> Latches;
364 L->getLoopLatches(Latches);
365
366 std::vector<BasicBlock*> Preds;
367 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000368 if (!is_contained(Latches, *PI))
Tom Stellard0f29de72015-02-05 15:32:15 +0000369 Preds.push_back(*PI);
370 }
Chandler Carruth96ada252015-07-22 09:52:54 +0000371 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false);
Tom Stellard0f29de72015-02-05 15:32:15 +0000372 }
373
Tom Stellardbc4497b2016-02-12 23:45:29 +0000374 Value *Exec = popSaved();
Changpeng Fang6b49fa42017-03-07 23:29:36 +0000375 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
376 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
377 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
Tom Stellardf8794352012-12-19 22:10:31 +0000378}
379
380/// \brief Annotate the control flow with intrinsics so the backend can
381/// recognize if/then/else and loops.
382bool SIAnnotateControlFlow::runOnFunction(Function &F) {
Tom Stellardbc4497b2016-02-12 23:45:29 +0000383
Chandler Carruth73523022014-01-13 13:07:17 +0000384 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tom Stellard0f29de72015-02-05 15:32:15 +0000385 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000386 DA = &getAnalysis<DivergenceAnalysis>();
Tom Stellardf8794352012-12-19 22:10:31 +0000387
388 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
389 E = df_end(&F.getEntryBlock()); I != E; ++I) {
390
391 BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator());
392
393 if (!Term || Term->isUnconditional()) {
394 if (isTopOfStack(*I))
395 closeControlFlow(*I);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000396
Tom Stellardf8794352012-12-19 22:10:31 +0000397 continue;
398 }
399
400 if (I.nodeVisited(Term->getSuccessor(1))) {
401 if (isTopOfStack(*I))
402 closeControlFlow(*I);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000403
Tom Stellardf8794352012-12-19 22:10:31 +0000404 handleLoop(Term);
405 continue;
406 }
407
408 if (isTopOfStack(*I)) {
409 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
410 if (Phi && Phi->getParent() == *I && isElse(Phi)) {
411 insertElse(Term);
412 eraseIfUnused(Phi);
413 continue;
414 }
415 closeControlFlow(*I);
416 }
417 openIf(Term);
418 }
419
420 assert(Stack.empty());
421 return true;
422}
423
424/// \brief Create the annotation pass
425FunctionPass *llvm::createSIAnnotateControlFlowPass() {
Tom Stellardf8794352012-12-19 22:10:31 +0000426 return new SIAnnotateControlFlow();
427}